AmitDiwan has Published 10744 Articles

Writing a For Loop to Evaluate a Factorial - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:34:47

1K+ Views

We are required to write a simple JavaScript function that takes in a Number, say n and computes its factorial using a for loop and returns the factorial.For example −factorial(5) = 120, factorial(6) = 720Maintain a count and a result variable, keep multiplying the count into result, simultaneously decreasing the ... Read More

Normalize numbers in an object - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:33:49

602 Views

Suppose, we have an object with strings mapped to numbers like this −const obj = {    num1: 45,    num2: 78,    num3: 234,    num4: 3,    num5: 79,    num6: 23 };We are required to write a JavaScript function that takes in one such object as the ... Read More

Implementing the Array.prototype.lastIndexOf() function in JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:32:11

279 Views

The lastIndexOf() function in JS returns the index of the very last occurrence of the element, passed into it as an argument, in the array, if it exists. If it does not exist the function returns -1.For example −[3, 5, 3, 6, 6, 7, 4, 3, 2, 1].lastIndexOf(3) would return ... Read More

Sum similar numeric values within array of objects - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:31:07

890 Views

Suppose, we have an array of objects like this −const arr = [    {"firstName":"John", "value": 89},    {"firstName":"Peter", "value": 151},    {"firstName":"Anna", "value": 200},    {"firstName":"Peter", "value": 22},    {"firstName":"Anna", "value": 60} ];We are required to write a JavaScript function that takes in one such array and combines the ... Read More

How to remove blank (undefined) elements from JavaScript array - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:29:52

695 Views

Suppose we have an array of literals like this −const arr = [4, 6, , 45, 3, 345, , 56, 6];We are required to write a JavaScript function that takes in one such array and remove all the undefined elements from the array in place. We are only required to ... Read More

Sort object array based on another array of keys - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:28:32

4K+ Views

Suppose, we have two arrays like these −const arr1 = ['d', 'a', 'b', 'c'] ; const arr2 = [{a:1}, {c:3}, {d:4}, {b:2}];We are required to write a JavaScript function that accepts these two arrays. The function should sort the second array according to the elements of the first array.We have ... Read More

How to subtract elements of two arrays and store the result as a positive array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:27:01

3K+ Views

Suppose, we have two arrays like these −const arr1 = [1, 2, 3, 4, 5, 6]; const arr2 = [9, 8, 7, 5, 8, 3];We are required to write a JavaScript function that takes in two such arrays and returns an array of absolute difference between the corresponding elements of ... Read More

Generating n random numbers between a range - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:25:46

254 Views

We are required to write a JavaScript function that takes in a number, say n, and an array of two numbers that represents a range. The function should return an array of n random elements all lying between the range provided by the second argument.ExampleFollowing is the code −const num ... Read More

Store count of digits in order using JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:24:34

112 Views

Suppose we have a string with digits like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in this string and returns an object that represents the count of each number in the string.Therefore, for this string, the output should be −const output = { ... Read More

Finding the sum of unique array values - JavaScript

AmitDiwan

AmitDiwan

Updated on 18-Sep-2020 13:23:33

990 Views

We are required to write a JavaScript function that takes in an array of numbers that may contain some duplicate numbers.Our function should return the sum of all the unique elements (elements that only appear once in the array) present in the array.For example −If the input array is −const ... Read More

Advertisements