AmitDiwan has Published 10744 Articles

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

158 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

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

AmitDiwan

AmitDiwan

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

447 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 ... Read More

Find first repeating character using JavaScript

AmitDiwan

AmitDiwan

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

677 Views

We have an array of string / number literals that may/may not contain repeating characters. Our job is to write a function that takes in the array and returns the index of the first repeating character. If the array contains no repeating characters, we should return -1.So, let’s write the ... Read More

How can I convert 'HH:MM:SS ' format to seconds in JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:23:26

340 Views

We are required to write a function that takes in a ‘HH:MM:SS’ string and returns the number of seconds. For example −countSeconds(‘12:00:00’) //43200 countSeconds(‘00:30:10’) //1810Let’s write the code for this. We will split the string, convert the array of strings into an array of numbers and return the appropriate number ... Read More

Compress array to group consecutive elements JavaScript

AmitDiwan

AmitDiwan

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

376 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, ... Read More

If string includes words in array, remove them JavaScript

AmitDiwan

AmitDiwan

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

788 Views

We are given a string and an array of strings; our job is to write a function that removes all the substrings that are present in the array from the string.These substrings are complete words so we are also supposed to remove leading or trailing whitespace so that no two ... Read More

Find average of each array within an array JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:19:40

211 Views

We are required to write a function getAverage() that accepts an array of arrays of numbers and we are required to return a new array of numbers that contains the average of corresponding subarrays.Let’s write the code for this. We will map over the original array, reducing the subarray to ... Read More

How to get only the first n% of an array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:19:16

246 Views

We are required to write a function that takes in an array arr and a number n between 0 and 100 (both inclusive) and returns the n% part of the array. Like if the second argument is 0, we should expect an empty array, complete array if it’s 100, half ... Read More

How to add a character to the beginning of every word in a string in JavaScript?

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:17:16

890 Views

We are required to write a function that takes in two strings, we have to return a new string which is just the same as the first of the two arguments but have second argument prepended to its every word.For example −Input → ‘hello stranger, how are you’, ‘@@’ Output ... Read More

Remove duplicates from a array of objects JavaScript

AmitDiwan

AmitDiwan

Updated on 20-Aug-2020 06:16:51

668 Views

We are required to write a function that removes duplicate objects from an array and returns a new one. Consider one object the duplicate of other if they both have same number of keys, same keys and same value for each key.Let’s write the code for this −We will use ... Read More

Advertisements