AmitDiwan has Published 10744 Articles

Reverse the words in the string that have an odd number of characters in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:43:01

520 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an odd number of characters in them.Any substring in the string qualifies to be a word, if either it is encapsulated by two spaces on either ends or ... Read More

Sum of distinct elements of an array in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:40:28

218 Views

Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example:The output ... Read More

Validate a number as Fibonacci series number in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:37:48

224 Views

We are required to write a JavaScript function that takes in a number and checks whether it falls in Fibonacci series or not.We should return a boolean on this basis.ExampleThe code for this will be −const num = 89; const isFib = query => {    if(query === 0 || ... Read More

Finding the first unique element in a sorted array in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:35:48

552 Views

Suppose we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array.If there ... Read More

Finding three desired consecutive numbers in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:31:39

238 Views

We are required to write a JavaScript function that takes in a Number, say n, and we are required to check whether there exist such three consecutive natural numbers (not decimal/floating point) whose sum equals to n.If there exist such numbers, our function should return them, otherwise it should return ... Read More

Reverse alphabetically sorted strings in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:29:45

323 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The ... Read More

Converting array of Numbers to cumulative sum array in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:28:15

576 Views

We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look ... Read More

Summing up all the digits of a number until the sum is one digit in JavaScript

AmitDiwan

AmitDiwan

Updated on 10-Oct-2020 07:26:27

1K+ Views

We are required to create a function that takes in a number and finds the sum of its digits recursively until the sum is a one-digit number.For examplefindSum(12345) = 1+2+3+4+5 = 15 = 1+5 = 6Therefore, the output should be 6.Let’s write the code for this function findSum()// using recursion ... Read More

How to remove some items from array when there is repetition in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 12:20:45

119 Views

We are required to write a JavaScript function that takes in an array of literals. Our function should return a new array with all the triplets filtered.The code for this will be −const arr1 = [1, 1, 1, 3, 3, 5]; const arr2 = [1, 1, 1, 1, 3, 3, ... Read More

Grouping objects based on key property in JavaScript

AmitDiwan

AmitDiwan

Updated on 09-Oct-2020 12:18:36

400 Views

We have a parentArray that contains many sub arrays each of the same size, each sub array is an array of objects containing two properties: key and value. Within a subarray it is confirmed that two objects cannot have the same key but all subarrays have the same pair of ... Read More

Advertisements