Found 9313 Articles for Object Oriented Programming

How to sum all elements in a nested array? JavaScript

AmitDiwan
Updated on 24-Aug-2020 06:11:08

1K+ Views

Let’s say, we are supposed to write a function that takes in a nested array of Numbers and returns the sum of all the numbers. We are required to do this without using the Array.prototype.flat() method.Let’s write the code for this function −Exampleconst arr = [    5,    7,    [ 4, [2], 8, [1,3], 2 ],    [ 9, [] ],    1,    8 ]; const findNestedSum = (arr) => {    let sum = 0;    for(let len = 0; len < arr.length; len++){       sum += Array.isArray(arr[len]) ? findNestedSum(arr[len]) :       arr[len];    };    return sum; }; console.log(findNestedSum(arr));OutputThe output in the console will be −50

How to compare two arrays in JavaScript and make a new one of true and false? JavaScript

AmitDiwan
Updated on 24-Aug-2020 06:09:43

397 Views

We have 2 arrays in JavaScript and we want to compare one with the other to see if the elements of master array exists in keys array, and then make one new array of the same length that of the master array but containing only true and false (being true for the values that exists in keys array and false the ones that don't).Let’s say, if the two arrays are −const master = [3, 9, 11, 2, 20]; const keys = [1, 2, 3];Then the final array should be −const finalArray = [true, false, false, true, false];Therefore, let’s write the ... Read More

Converting string to MORSE code in JavaScript

AmitDiwan
Updated on 24-Aug-2020 06:08:04

3K+ Views

What is Morse code?Morse code is a method used in telecommunications to encode text characters as standardized sequences of two different signal durations, called dots and dashes.To have a function that converts a particular string to Morse code, we will need an object that maps all the characters (English alphabets) to Morse code equivalents. Once we have that we can simply iterate over the string and construct a new string.Here is the object that maps alphabets to Morse codes −Morse Code Mapconst morseCode = {    "A": ".-",    "B": "-...",    "C": "-.-.",    "D": "-..",    "E": ".", ... Read More

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

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

198 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 number is greater than 9 or lesser than -9 (we will take care of sign separately so that we don’t have to write the logic twice)Exampleconst sumRecursively = (n, isNegative = n < 0) => {    n = Math.abs(n);    if(n > 9){       return sumRecursively(parseInt(String(n).split("").reduce((acc, val) ... Read More

JavaScript Find the first non-consecutive number in Array

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

262 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 the numbers are in perfect order, we should return -1.Let’s write the code for this function −Exampleconst arr = [1, 2, 3, 4, 5, 6, 8, 9, 10]; const secondArr = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const findException = (arr) => { ... Read More

JavaScript construct an array with elements repeating from a string

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

120 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 for this function −Exampleconst string = 'Hello'; const limit = 15; const createStringArray = (string, limit) => {    const arr = [];    for(let i = 0; i < limit; i++){       const index = i % string.length;       arr.push(string[index]);    };    return arr; ... Read More

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

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

201 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 up to the target, and if no two elements add up to the target, our function should return an empty array.Solving the problem in O(n) timeWe will use a hashmap to keep a record of the items already appeared, on each pass we will check whether there exists any element ... Read More

Loop through array and edit string JavaScript

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

632 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 replace the corresponding places.For example −If the function call is like this −translate(‘This $0 is more $1 just a $2.’, ‘game’, ‘than’, ‘game’);The output of the function should be −This game is more than just a game.This functionality is more or less like the template injecting in JavaScript.Therefore, let’s write ... Read More

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

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

259 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 arr = [34, 6, 76, 0, 0, 343, 90, 0, 32, 0, 34, 21, 54]; const moveZero = (arr) => {    for(ind = 0; ind < arr.length; ind++){       const el = arr[ind];       if(el === 0){          arr.push(arr.splice(ind, 1)[0]);          ind--;       };    } }; moveZero(arr); console.log(arr);OutputThe output in the console will be −[34, 6, 76, 343, 90, 32, 34, 21, 54, 0, 0, 0, 0]

Recursive product of summed digits JavaScript

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

146 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 keep multiplying the digits together until we get a 1-digit number like this −9*2 = 18 1*8 = 8When we get the one-digit number, we have to return it from our function.We will break this into two functions −One function accepts a number and returns the product of its digits, ... Read More

Advertisements