AmitDiwan has Published 10740 Articles

Repeated sum of Number’s digits in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 12:54:30

376 Views

We are required to write a JavaScript function that recursively sums up the digits of a number until it reduces to a single digit number.We are required to do so without converting the number to String or any other data type.Therefore, let’s write the code for this function −ExampleThe code ... Read More

Taking common elements from many arrays in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 12:50:49

539 Views

We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.Therefore, let’s write the code for this function −ExampleThe code ... Read More

Summing up digits and finding nearest prime in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 12:49:13

137 Views

We are required to write a JavaScript function that takes in a number, finds the sum of its digits and returns a prime number that is just greater than or equal to the sum.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = ... Read More

Find the Smallest element from a string array in JavaScript

AmitDiwan

AmitDiwan

Updated on 22-Oct-2020 12:46:07

332 Views

We are required to write a JavaScript function that takes in an array of strings and returns the index of string that is shortest in length.We will simply use a for loop and persist the index of string which is shortest in length.Therefore, let’s write the code for this function ... Read More

Adding up identical elements in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:42:29

125 Views

We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one index.For exampleIf the input array is −const arr = [20, 10, 15, 20, 15, 10];Then the output should be −const output = [40, 20, 30];Therefore, let’s ... Read More

Merging two arrays in a unique way in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:41:09

169 Views

We are required to write a JavaScript function that takes in two arrays and merges the arrays taking elements alternatively from the arrays.For exampleIf the two arrays are −const arr1 = [4, 3, 2, 5, 6, 8, 9]; const arr2 = [2, 1, 6, 8, 9, 4, 3];Then the output ... Read More

Picking out uniques from an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:37:34

158 Views

Suppose we have an array that contains duplicate elements like this −const arr = [1, 1, 2, 2, 3, 4, 4, 5];We are required to write a JavaScript function that takes in one such array and returns a new array. The array should only contain the elements that only appear ... Read More

Non-composite numbers sum in an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:36:18

183 Views

We are required to write a JavaScript function that takes in an array of numbers.The function should return the sum of all the prime numbers present in the array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [43, 6, 6, 5, 54, ... Read More

Inverting slashes in a string in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:34:40

387 Views

We are required to write a JavaScript function that takes in a string that may contain some backward slashes.And the function should return a new string where all the backslashes with forward slashes.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th/s ... Read More

Finding sort order of string in JavaScript

AmitDiwan

AmitDiwan

Updated on 21-Oct-2020 12:33:23

214 Views

We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.For example:isSorted('adefgjmxz') // true isSorted('zxmfdba') // true isSorted('dsfdsfva') // falseTherefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'abdfhlmxz'; const findDiff = ... Read More

Advertisements