Javascript Articles - Page 332 of 534

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

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

531 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 football' }, {    id: 125,    hobby: 'cooking' }, {    id: 129,    hobby: 'horse riding' }];Let’s say, we have to write a function that takes in such an array and merges it based on the common id property, and for the hobby property we assign it an ... Read More

Determining happy numbers using recursion JavaScript

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

827 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 because, 1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1On the other hand, 36 is an unhappy number.We are required to write a function that uses recursion to determine whether or not a number is a happy number.So, let’s write this function out. The key to this function ... Read More

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

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

437 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, 6, 12, 7, 1] = 5 + 6 + 12 + 7 + 1 = 31; [6, 12, 7, 1] = 6 + 12 + 7 + 1 = 26; [12, 7, 1] = 12 + 7 + 1 = 20; [7, 1] = 7 + 1 = 8; [1] ... Read More

How to slice an array with wrapping in JavaScript

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

391 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 slice() function like so it returns a subarray from index start to end and not end-1. Therefore, the code for doing this is shown below. We iterate over the array using a for loop which is in fact is faster than any of the array methods we have. Then return ... Read More

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

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

399 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 return the index that makes most appearances.The code for doing so will be −Exampleconst arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ... Read More

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

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

399 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 is str2. str1 is 'cashwool' and str2 is ‘school’ the output should return true. str1 is 'katas' and str2 is 'steak' should return false.So, here is the code for doing this. We simply split and sort the two strings and then check whether the smaller string is a substring of ... Read More

How to calculate total time between a list of entries?

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

215 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: 'downstream',    velocity: 50 }, {    direction: 'upstream',    velocity: 35 }, {    direction: 'downstream',    velocity: 25 }, {    direction: 'upstream',    velocity: 40 }, {    direction: 'upstream',    velocity: 37.5 }]We are required to write a function that takes in such type of array ... Read More

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

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

192 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 code for this function −Exampleconst arr = [10, 60]; const sumUpto = (n) => (n*(n+1))/2; const sumBetween = (array) => {    if(array.length !== 2){       return -1;    }    const [a, b] = array;    return sumUpto(Math.max(a, b)) - sumUpto(Math.min(a, b)) + Math.min(a,b); }; console.log(sumBetween(arr)); console.log(sumBetween([4, 9]));OutputThe output in the console will be −1785 39

Recursion in array to find odd numbers and push to new variable JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:25:26

477 Views

We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used.Exampleconst arr = [12, 4365, 76, 43, 76, 98, 5, 31, 4]; const pushRecursively = (arr, len = 0, odd = [], even = []) => {    if(len < arr.length){       arr[len] % 2 === ... Read More

Compress array to group consecutive elements JavaScript

AmitDiwan
Updated on 20-Aug-2020 06:22:33

432 Views

We are given a string that contains some repeating words separated by dash (-) like this −const str = 'monday-sunday-tuesday-tuesday-sunday-sunday-monday-mondaymonday';Now our job is to write a function that returns an array of objects, where each object contains two properties value and count, value is the word in the string (Monday, Tuesday, Sunday) and count is their consecutive appearance count.Like for the above string, this array would look something like this −const arr = [{    val: 'monday',    count: 1 }, {    val: 'sunday',    count: 1 }, {    val: 'tuesday',    count: 2 }, {    val: ... Read More

Advertisements