AmitDiwan has Published 10740 Articles

How to reduce an array while merging one of its field as well in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:39:58

526 Views

Consider, we have the following array of objects −const arr = [{    id: 121,    hobby: 'cycling' }, {    id: 125,    hobby: 'jogging' }, {    id: 129,    hobby: 'reading' }, {    id: 121,    hobby: 'writing' }, {    id: 121,    hobby: 'playing ... Read More

Determining happy numbers using recursion JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:37:52

802 Views

Happy NumberA happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers.For example − 13 is a happy number ... Read More

Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:37:20

425 Views

Consider the following array of numbers −const arr = [10, 5, 6, 12, 7, 1];The sum of its consecutive elements taking one less element in every go will be −[10, 5, 6, 12, 7, 1] = 10 + 5 + 6 + 12 + 7 + 1 = 41; [5, ... Read More

How to slice an array with wrapping in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:35:28

387 Views

Let’s say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1.What we wish to do is make this ... Read More

Get the item that appears the most times in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:34:53

388 Views

Let’s say, we are required to write a function that takes in an array of string / number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequencyMap and from that map we will ... Read More

How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:33:08

387 Views

We have to write a function that returns true if a portion of string1 can be rearranged to string2. Write function, say scramble(str1, str2) that returns true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.For example −Let’s say string1 is str1 and string2 ... Read More

How to calculate total time between a list of entries?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:32:12

208 Views

Let’s say, we have an array that contains some data about the speed of a motor boat during upstreams and downstreams like this −Following is our sample array −const arr = [{    direction: 'upstream',    velocity: 45 }, {    direction: 'downstream',    velocity: 15 }, {    direction: ... Read More

JavaScript Splitting string by given array element

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:31:10

209 Views

Let’s say, we are given a string and an array. Our job is to split the string according to the corresponding elements of the array. For example −Inputconst string = 'Javascript splitting string by given array element'; const arr = [2, 4, 5, 1, 3, 1, 2, 3, 7, 2];Output['Ja', ... Read More

Randomize color by number in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:28:08

161 Views

We are required to write a function that returns a random hex color. So here is the code for doing so −Exampleconst generateRandomColor = () => {    const keys = '0123456789ABCDEF';    let color = '';    while(color.length < 6){       const random = Math.floor(Math.random() * 16); ... Read More

Sum is to be calculated for the numbers in between the array's max and min value JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:27:00

184 Views

We are required to write a function, say sumBetween() that takes in an array [a, b] of two elements and returns the sum of all the elements between a and b including a and b.For example −[4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40Let’s write the ... Read More

Advertisements