AmitDiwan has Published 10744 Articles

Recursively flat an object JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:38:41

391 Views

We are required to write a function that does the following transformation −If the input object is −const input = {    a: 0,    b: {x: {y: 1, z: 2}},    c: 3 };Then the output of the function should be −const output = {    a: 0,   ... Read More

Parse array to equal intervals in JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:36:25

209 Views

Let’s say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so ... Read More

How to check if every property on object is the same recursively in JavaScript?

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:34:21

427 Views

Let’s say, we are required to write a function, say isSame() that accepts a nested object and returns a boolean depending on the fact whether or not all the keys have the same values. When saying all the keys we mean all the last keys like if a key has ... Read More

Rearrange an array in maximum minimum form by JavaScript

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:31:44

268 Views

We are required to write a function, say minMax() that takes in an array of Numbers and rearranges the elements such that the greatest element appears first followed by the smallest elements then the second greatest element followed by second smallest element and so on.For example −// if the input ... Read More

How to count the number of elements in an array below/above a given number (JavaScript)

AmitDiwan

AmitDiwan

Updated on 26-Aug-2020 09:30:08

209 Views

Consider we have an array of Numbers that looks like this −const array = [3.1, 1, 2.2, 5.1, 6, 7.3, 2.1, 9];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is ... Read More

Group values in array by two properties JavaScript

AmitDiwan

AmitDiwan

Updated on 25-Aug-2020 07:56:05

1K+ Views

We have an array of objects like this −const arr = [    { value: 12, gap: 1 },    { value: 13, gap: 1 },    { value: 14, gap: 1 },    { value: 15, gap: 1 },    { value: 19, gap: 2 },    { value: ... Read More

Form Object from string in JavaScript

AmitDiwan

AmitDiwan

Updated on 25-Aug-2020 07:53:01

264 Views

We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0.For example −// if the input string is: ... Read More

Return an array populated with the place values of all the digits of a number in JavaScript

AmitDiwan

AmitDiwan

Updated on 25-Aug-2020 07:51:47

185 Views

We are required to write a function splitNumber() that takes in a positive integer and returns an array populated with the place values of all the digits of the number.For example −//if the input is: const num = 2346; //the output should be: const output = [2000, 300, 40, 6];Let’s ... Read More

Group array by equal values JavaScript

AmitDiwan

AmitDiwan

Updated on 25-Aug-2020 07:43:26

1K+ Views

Let’s say, we have an array of string / number literals that contains some duplicate values like this −const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night'];We are required to write a function groupSimilar() that takes in this array and returns a new array where ... Read More

Changing an array in place using splice() JavaScript

AmitDiwan

AmitDiwan

Updated on 25-Aug-2020 07:41:48

185 Views

We are required to write a function that, given an array arr and a number n, returns the array with elements repeating no more than n times. And we have to do all this without disturbing the indices of desired elements. So, let’s write the code for this function, We ... Read More

Advertisements