Found 10483 Articles for Web Development

How to combine 2 arrays into 1 object in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:51:33

755 Views

Let’s say, we have two arrays of equal lengths and are required to write a function that maps the two arrays into an object. The corresponding elements of the first array becomes the corresponding keys of the object and the elements of the second array become the value.We will reduce the first array, at the same time accessing elements of the second array by index. The code for this will be −Exampleconst keys = [    'firstName',    'lastName',    'isEmployed',    'occupation',    'address',    'salary',    'expenditure' ]; const values = [    'Hitesh',    'Kumar',    false, ... Read More

Make numbers in array relative to 0 – 100 in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:49:40

238 Views

Let’s say, we have an array that contains some numbers, our job is to write a function that takes in the array and maps all the values relative to 0 to 100. Means that the greatest number should get replaced by 100, the smallest by 100 and all others should get converted to specific numbers between 0 and 100 according to the ratio.Following is the code for doing the same −Exampleconst numbers = [45.71, 49.53, 18.5, 8.38, 38.43, 28.44]; const mapNumbers = (arr) => {    const max = Math.max(...arr);    const min = Math.min(...arr);    const diff = max ... Read More

Positive integer square array JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:46:49

335 Views

Let’s say, we have an array that contains some numbers, positive, negative, decimals and integers. We have to write a function that takes in an array and returns an array of square of all the positive integers from the original array.Let’s write the code for this function −Exampleconst arr = [1, -4, 6.1, 0.1, 2.6, 5, -2, 1.9, 6, 8.75, -7, 5]; const squareSum = (arr) => {    return arr.reduce((acc, val) => {       //first condition checks for positivity and second for wholeness of the number       if(val > 0 && val % 1 === 0){          acc += val*val;       };       return acc;    },0); } console.log(squareSum(arr));OutputThe output in the console will be −87

How to splice duplicate item in array JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:42:16

670 Views

We have an array of Number / String literals that contain some duplicate values, we have to remove these values from the array without creating a new array or storing the duplicate values anywhere else.We will use the Array.prototype.splice() method to remove entries inplace, and we will take help of Array.prototype.indexOf() and Array.prototype.lastIndexOf() method to determine the duplicacy of any element.Exampleconst arr = [1, 4, 6, 1, 2, 5, 2, 1, 6, 8, 7, 5]; arr.forEach((el, ind, array) => {    if(array.indexOf(el) !== array.lastIndexOf(el)){       array.splice(ind, 1);    } }); console.log(arr);OutputThe output in the console will be −[    4, 1, 5, 2,    6, 8, 7 ]

Sum from array values with similar key in JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:34:47

541 Views

Let’s say, here is an array that contains some data about the stocks sold and purchased by some company over a period of time.const transactions = [    ['AAPL', 'buy', 100],    ['WMT', 'sell', 75],    ['MCD', 'buy', 125],    ['GOOG', 'sell', 10],    ['AAPL', 'buy', 100],    ['AAPL', 'sell', 100],    ['AAPL', 'sell', 20],    ['DIS', 'buy', 15],    ['MCD', 'buy', 10],    ['WMT', 'buy', 50],    ['MCD', 'sell', 90] ];We want to write a function that takes in this data and returns an object of arrays with key as stock name (like ‘AAPL’, ‘MCD’) and value as array ... Read More

Fill missing numeric values in a JavaScript array

AmitDiwan
Updated on 21-Aug-2020 13:28:23

515 Views

We are given an array of n entries, of which only 2 are Numbers, all other entries are null. Something like this −const arr = [null, null, -1, null, null, null, -3, null, null, null];We are supposed to write a function that takes in this array and complete the arithmetic series of which these two numbers are a part. To understand this problem more clearly, we can think of these null values as blank space where we need to fill numbers so that the whole array forms an arithmetic progression.About Arithmetic ProgressionA series / array of numbers is said to ... Read More

Returning an array with the minimum and maximum elements JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:22:00

144 Views

Provided an array of numbers, let’s say −const arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76];We are required to write a function that picks the minimum and maximum element from the array and returns an array of those two numbers with minimum at index 0 and maximum at 1.We will use the Array.prototype.reduce() method to build a minimum maximum array like this −Exampleconst arr = [12, 54, 6, 23, 87, 4, 545, 7, 65, 18, 87, 8, 76]; const minMax = (arr) => {    return arr.reduce((acc, val) => {       if(val < acc[0]){          acc[0] = val;       }       if(val > acc[1]){          acc[1] = val;       }       return acc;    }, [Infinity, -Infinity]); }; console.log(minMax(arr));OutputThe output in the console will be −[ 4, 545 ]

Reverse digits of an integer in JavaScript without using array or string methods

AmitDiwan
Updated on 21-Aug-2020 13:18:25

392 Views

We are required to write Number.prototype.reverse() function that returns the reversed number of the number it is used with.For example −234.reverse() = 432; 6564.reverse() = 4656;Let’s write the code for this function. We will use a recursive approach like this −Exampleconst reverse = function(temp = Math.abs(this), reversed = 0, isNegative = this < 0){    if(temp){       return reverse(Math.floor(temp/10), (reversed*10)+temp%10,isNegative);    };    return !isNegative ? reversed : reversed*-1; }; Number.prototype.reverse = reverse; const n = -12763; const num = 43435; console.log(num.reverse()); console.log(n.reverse());OutputThe output in the console will be −53434 -36721

Digital root sort algorithm JavaScript

AmitDiwan
Updated on 21-Aug-2020 13:15:03

258 Views

Digit root of some positive integer is defined as the sum of all of its digits. We are given an array of integers. We have to sort it in such a way that if a comes before b if the digit root of a is less than or equal to the digit root of b. If two numbers have the same digit root, the smaller one (in the regular sense) should come first. For example, 4 and 13 have the same digit root, however 4 < 13 thus 4 comes before 13 in any digitRoot sorting where both are present.For ... Read More

Sort array by year and month JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:48:36

3K+ Views

We have an array like this −const arr = [{    year: 2020,    month: 'January' }, {    year: 2017,    month: 'March' }, {    year: 2010,    month: 'January' }, {    year: 2010,    month: 'December' }, {    year: 2020,    month: 'April' }, {    year: 2017,    month: 'August' }, {    year: 2010,    month: 'February' }, {    year: 2020,    month: 'October' }, {    year: 2017,    month: 'June' }]We have to sort this array according to years in ascending order (increasing order). Moreover, if there exist two objects ... Read More

Advertisements