Found 6710 Articles for Javascript

Return an array of all the indices of minimum elements in the array in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 11:59:23

952 Views

An Array in JavaScript is a single variable where it can store different elements. These elements are stored at contiguous memory locations. Array elements can be accessed with the help of index numbers. The index numbers will start from 0. Syntax Below is the basic declaration of the array in JavaScript − Cosnt cars = [Maruti, Hyundai, Honda]; We need to return an array of all the indices of minimum elements in a JavaScript array Let’s look into the input-output scenario Assume there is an integer array where the minimum element is repeated more than once. We need to ... Read More

Function to compute factorial of a number in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:24:16

13K+ Views

In this article, the given task is to get the factorial of a number in JavaScript. What is the factorial of a number? The multiplication of all positive integers smaller than or equal to n gives the factorial of a non-negative integer. For example, assume the non-negative integer is 4 and the factorial of 4 will be 4*3*2*1 which is equal to 24. The symbol of factorial is represented by "!". so it will be (4!). The value of 0! Will be 1 as 0 is not a positive integer. The mathematical formula of factorial ("n!") This is ... Read More

Removing all the empty indices from array in JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:06:35

1K+ Views

Given there is an array with some empty indices and need to remove those empty indices from array. Let’s look into the input output scenarios – Consider there is an array with some empty indices. Now we need to exclude them and return the elements which are having only truthy values. Array = [22, 45, , 56, 71, , 10]; Output = [22, 45, 56, 71, 10] As we can see in the output, the indices which are empty in the array got removed. We can achieve the above task by using several methods, let’s look into the ... Read More

Sort the second array according to the elements of the first array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:04:17

224 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 to sort the keys of the second array according to the elements of the first array.Therefore, the output should look like −const output = [{d:4}, {a:1}, {b:2}, {c:3}];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['d', 'a', 'b', 'c'] ; const ... Read More

Absolute difference of Number arrays in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:02:46

461 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 such two arrays and returns an array of absolute difference between the corresponding elements of the array.So, for these arrays, the output should look like −const output = [8, 6, 4, 1, 3, 3];We will use a for loop and keep pushing the absolute difference iteratively into a new array and finally return the array.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Outputting a random number that falls in a certain range in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:00:13

112 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.Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 10; const range = [5, 15]; const randomBetween = (a, b) => {    return ((Math.random() * (b - a)) + a).toFixed(2); }; const randomBetweenRange = (num, range) => {    const res = [];    for(let i = ... Read More

Constructing an object from repetitive numeral string in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:58:45

127 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.So, for this string, the output should be −const output = {    "1": 2,    "2": 4,    "3": 3,    "4": 7,    "5": 1,    "6": 3 };Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = '11222233344444445666'; const mapString = str => {    const map = {};    for(let i ... Read More

Summing up unique array values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:57:06

154 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 exampleIf the input array is −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, 11];Then the output should be 25.We will simply use a for loop, iterate the array and return the sum of unique elements.ExampleThe code for this will be −const arr = [2, 5, 5, 3, 2, 7, 4, 9, 9, ... Read More

Add matching object values in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:55:11

363 Views

Suppose, we have an array of objects like this −const arr = [{a: 2, b: 5, c: 6}, {a:3, b: 4, d:1}, {a: 1, d: 2}];Each object is bound to have unique in itself (for it to be a valid object), but two different objects can have the common keys (for the purpose of this question).We are required to write a JavaScript function that takes in one such array and returns an object with all the unique keys present in the array and their values cumulative sum as the value.So, the resultant object should look like −const output = {a: ... Read More

Sorting a JSON object in JavaScript

AmitDiwan
Updated on 21-Oct-2020 11:53:46

4K+ Views

Suppose we have an object like this −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 };We are required to write a JavaScript function that takes in this object and returns a sorted array like this −const arr = [11, 23, 56, 67, 88];Here, we sorted the object values and placed them in an array.Therefore, let’s write the code for this function −ExampleThe code for this will be −const obj = {    key1: 56,    key2: 67,    key3: 23,    key4: 11,    key5: 88 }; const sortObject ... Read More

Advertisements