AmitDiwan has Published 10740 Articles

Code to implement bubble sort - JavaScript

AmitDiwan

AmitDiwan

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

264 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 ... Read More

Calculate compound interest in JavaScript

AmitDiwan

AmitDiwan

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

6K+ 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, ... Read More

Split number into n length array - JavaScript

AmitDiwan

AmitDiwan

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

376 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; ... Read More

Palindrome array - JavaScript

AmitDiwan

AmitDiwan

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

239 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]; ... Read More

Add two array keeping duplicates only once - JavaScript

AmitDiwan

AmitDiwan

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

125 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 ... Read More

Object to array - JavaScript

AmitDiwan

AmitDiwan

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

348 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 ... Read More

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

AmitDiwan

AmitDiwan

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

413 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 ... Read More

Switch case calculator in JavaScript

AmitDiwan

AmitDiwan

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

8K+ 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 ... Read More

Summing numbers from a string - JavaScript

AmitDiwan

AmitDiwan

Updated on 15-Sep-2020 09:06:43

944 Views

We are required to write a JavaScript function that takes in a string that contains some one-digit numbers in between and the function should return the sum of all the numbers present in the string.Let’s say the following is our string with numbers −const str = 'gdf5jhhj3hbj4hbj3jbb4bbjj3jb5bjjb5bj3';ExampleLet's write the code ... Read More

Alternate addition multiplication in an array - JavaScript

AmitDiwan

AmitDiwan

Updated on 15-Sep-2020 09:04:14

268 Views

We are required to write a JavaScript function that takes in an array of numbers and returns the alternative multiplicative sum of the elementsFor example −If the array is −const arr = [1, 2, 4, 1, 2, 3, 4, 3];then the output should be calculated like this −1*2+4*1+2*3+4*3 2+4+6+12And the ... Read More

Advertisements