Found 6683 Articles for Javascript

Finding upper elements in array in JavaScript

AmitDiwan
Updated on 21-Oct-2020 12:24:44

99 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a single number as the second argument. The function should return an array of all the elements from the input array that are greater than or equal to the number taken as the second argument.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5]; const threshold = 40; const findGreater = (arr, num) => {    const res = ... Read More

Maps in JavaScript takes keys and values array and maps the values to the corresponding keys

AmitDiwan
Updated on 21-Oct-2020 12:22:55

122 Views

Suppose we have two arrays −const keys = [0, 4, 2, 3, 1]; const values = ["first", "second", "third", "fourth", "fifth"];We are required to write a JavaScript function that takes in the keys and the values array and maps the values to the corresponding keys.Therefore, the output should look like −const map = {    0 => 'first',    4 => 'second',    2 => 'third',    3 => 'fourth',    1 => 'fifth' };Therefore, let’s write the code for this function −ExampleThe code for this will be −const keys = [0, 4, 2, 3, 1]; const values = ["first", ... Read More

Converting array of objects to an object in JavaScript

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

374 Views

The given there is an array and the task is to convert an array of objects to an object. Input-Output Scenario Let’s look into an input-output scenario of converting the array objects into an object. Consider there is an array having objects in it. Now, we need to convert those array objects into a single JSON object. Const Arr = [ {1:'one'}, {2:'two'} ]; Output = { "1":"one", "2":"two" }; //Object Using Object.assign() The ... Read More

Generating random hex color in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:00:55

2K+ Views

The Hex (Hexadecimal) code is a six-digit code and a three-byte hexadecimal number that is used to represent the colors. These three bytes represent RGB which means the amount of red, green, and blue in a particular shade of a color. Each byte will represent a number in the range 00 to FF (Hexadecimal notation) or 0 to 255 in decimal notation. This indicates the intensity of each of the color components from 0 to 255. The following are the functions to be used for generating random hex codes − Math.random() is used to get the number randomly in the ... Read More

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

692 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

10K+ 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

132 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

373 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

80 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

Advertisements