AmitDiwan has Published 10744 Articles

Reduce sum of digits recursively down to a one-digit number JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 06:05:04

265 Views

We have to write a function that takes in a number and keeps adding its digit until the result is not a one-digit number, when we have a one-digit number, we return it.The code for this is pretty straightforward, we write a recursive function that keeps adding digit until the ... Read More

JavaScript Find the first non-consecutive number in Array

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 06:02:50

343 Views

We have to write a function that takes in an array and returns the index of the first nonconsecutive number from it. Like all the numbers will be in an arithmetic progression of common difference 1. But the number, which violates this rule, we have to return its index.If all ... Read More

JavaScript construct an array with elements repeating from a string

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 06:00:57

161 Views

We have to write a function that creates an array with elements repeating from the string till the limit is reached. Suppose there is a string ‘aba’ and a limit 5 −string = "aba" and limit = 5 will give new array ["a", "b", "a", "a", "b"]Let’s write the code ... Read More

Is there any more efficient way to code this “2 Sum” Questions JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:57:02

270 Views

Our job is to write a function that solves the two-sum problem in at most linear time.Two Sum ProblemGiven an array of integers, we have to find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers that add ... Read More

Loop through array and edit string JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:55:16

761 Views

Let’s say, we have to write a function, say translate() that accepts a string as the first argument and any number of words after that.The string will actually contain n $ signs like this −This $0 is more $1 just a $2. Then there will be 3 strings which will ... Read More

Write an algorithm that takes an array and moves all of the zeros to the end JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:53:19

300 Views

We have to write a function that takes in an array and moves all the zeroes present in that array to the end of the array without using any extra space. We will use the Array.prototype.forEach() method here along with Array.prototype.splice() and Array.prototype.push().The code for the function will be −Exampleconst ... Read More

Recursive product of summed digits JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:51:42

200 Views

We have to create a function that takes in any number of arguments (Number literals), adds them together, and returns the product of digits when the answer is only 1 digit long.For example −If the arguments are −16, 34, 42We have to first add them together −16+34+42 = 92And then ... Read More

JavaScript code for recursive Fibonacci series

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:49:10

378 Views

We have to write a recursive function fibonacci() that takes in a number n and returns an array with first n elements of fibonacci series. Therefore, let’s write the code for this function −Exampleconst fibonacci = (n, res = [], count = 1, last = 0) => {    if(n){ ... Read More

Get the longest and shortest string in an array JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:47:08

718 Views

We have an array of string literals like this −const arr = ['Some', 'random', 'words', 'that', 'actually', 'form', 'a', 'sentence.'];We are required to write a function that returns the longest and the shortest word from this array. We will use Array.prototype.reduce() method to keep track of the longest and shortest ... Read More

Sum of consecutive numbers in JavaScript

AmitDiwan

AmitDiwan

Updated on 24-Aug-2020 05:43:58

507 Views

Let’s say, we have to write a function that takes in an array and returns another array in which the consecutive similar numbers are added up together.For example −const array = [1, 5, 5, 5, 8, 8, 9, 1, 4, 4, 2];The output should be −[1, 15, 16, 9, 1, ... Read More

Advertisements