Found 9316 Articles for Object Oriented Programming

Calculate difference between circumference and area of a circle - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:30:50

141 Views

Circumference of a circle is given by −pi * (r * r)And area of a circle is given by −2 * pi * rWhere r is the radius of the circle.We are required to write a JavaScript function that takes in the radius of circle and calculates the difference between the area and the circumference of the circleExampleLet’s write the code for this function −const rad = 6; const circleDifference = radius => {    const area = Math.PI * (radius * radius);    const circumference = 2 * Math.PI * radius;    const diff = Math.abs(area - circumference);   ... Read More

Program to find largest of three numbers - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:29:09

425 Views

We are required to write a JavaScript function that takes in three or more numbers and returns the largest of those numbers.For example: If the input numbers are −4, 6, 7, 2, 3Then the output should be −7ExampleLet’s write the code for this function −// using spread operator to cater any number of elements const findGreatest = (...nums) => {    let max = -Infinity;    for(let i = 0; i < nums.length; i++){       if(nums[i] > max){          max = nums[i];       };    };    return max; }; console.log(findGreatest(5, 6, 3, 5, 7, 5));OutputThe output in the console −7

Code to implement bubble sort - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:25:25

140 Views

We are required to write a JavaScript function that takes in an array of literals and sorts it using bubble sort. In Bubble Sort, each pair of adjacent elements is compared and the elements are swapped if they are not in order.ExampleLet’s write the code for this function −const arr = [4, 56, 4, 23, 8, 4, 23, 2, 7, 8, 8, 45]; const swap = (items, firstIndex, secondIndex) => {    var temp = items[firstIndex];    items[firstIndex] = items[secondIndex];    items[secondIndex] = temp; }; const bubbleSort = items => {    var len = items.length,    i, j;   ... Read More

Calculate compound interest in JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:23:00

5K+ Views

Compound Interest FormulaCompound interest is calculated using the following formula −CI = P*(1 + R/n) (nt) – PHere, P is the principal amount.R is the annual interest rate.t is the time the money is invested or borrowed for.n is the number of times that interest is compounded per unit t, for example if interest is compounded monthly and t is in years then the value of n would be 12. If interest is compounded quarterly and t is in years then the value of n would be 4.We are required to write a JavaScript function that takes in principal, rate, ... Read More

Split number into n length array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:20:57

281 Views

We are required to write a JavaScript function that takes in two numbers say m and n, and returns an array of size n with all the elements of the resulting array adding up to m.Let’s write the code for this function −ExampleFollowing is the code −const len = 8; const sum = 5; const splitNumber = (len, sum) => {    const res = [];    for(let i = 0; i < len; i++){       res.push(sum / len);    };    return res; }; console.log(splitNumber(len, sum));OutputThe output in the console: −[    0.625, 0.625,    0.625, 0.625,    0.625, 0.625,    0.625, 0.625 ]

Palindrome array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:18:38

165 Views

We are required to write a JavaScript function that takes in an array of literals and checks if elements are the same or not if read from front or back i.e. palindrome.ExampleLet’s write the code for this function −const arr = [1, 5, 7, 4, 15, 4, 7, 5, 1]; const isPalindrome = arr => {    const { length: l } = arr;    const mid = Math.floor(l / 2);    for(let i = 0; i

Add two array keeping duplicates only once - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:16:58

58 Views

Suppose, we have two arrays of literals like these :const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6];We are required to write a JavaScript function that takes in two such arrays and returns a new array with all the duplicates removed (should appear only once).ExampleLet’s write the code for this function −const arr1 = [2, 4, 5, 3, 7, 8, 9]; const arr2 = [1, 4, 5, 2, 3, 7, 6]; const mergeArrays = (first, second) => {    const { length: l1 } = first;    const { ... Read More

Object to array - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:13:11

259 Views

Suppose, we have an object of key value pairs like this −const obj = {    name: "Vikas",    age: 45,    occupation: "Frontend Developer",    address: "Tilak Nagar, New Delhi",    experience: 23,    salary: "98000" };We are required to write a function that takes in the object and returns an array of arrays with each subarray representing one key value pairExampleLet’s write the code for this function −const obj = {    name: "Vikas",    age: 45,    occupation: "Frontend Developer",    address: "Tilak Nagar, New Delhi",    experience: 23,    salary: "98000" }; const objectToArray = obj ... Read More

Finding difference of greatest and the smallest digit in a number - JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:10:13

291 Views

We are required to write a JavaScript function that takes in a number and returns the difference between the greatest and the smallest digit present in it.For example: If the number is 5464676, then the smallest digit here is 4 and the greatest is 7Hence, our output should be 3ExampleLet’s write the code for this function −const num = 44353456; const difference = (num, min = Infinity, max = -Infinity) => {    if(num){       const digit = num % 10;       return difference(Math.floor(num / 10), Math.min(digit, min),       Math.max(digit, max));    };    return max - min; }; console.log(difference(num));OutputThe output in the console: −3

Switch case calculator in JavaScript

AmitDiwan
Updated on 15-Sep-2020 09:08:43

7K+ Views

Let’s say, we are required to write a JavaScript function that takes in a string like these to create a calculator −"4 add 6" "6 divide 7" "23 modulo 8"Basically, the idea is that the string will contain two numbers on either sides and a string representing the operation in the middle.The string in the middle can take one of these five values −"add", "divide", "multiply", "modulo", "subtract"Our job is to return the correct result based on the stringExampleLet’s write the code for this function −const problem = "3 add 16"; const calculate = opr => {    const [num1, ... Read More

Advertisements