Javascript Articles

Page 430 of 534

Check if values of two arrays are the same/equal in JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 691 Views

We have two arrays of numbers, let’s say −[2, 4, 6, 7, 1] [4, 1, 7, 6, 2]Assume, we have to write a function that returns a boolean based on the fact whether or not they contain the same elements irrespective of their order.For example −[2, 4, 6, 7, 1] and [4, 1, 7, 6, 2] should yield true because they have the same elements but ordered differently.Now, let’s write the code for this function −Exampleconst first = [2, 4, 6, 7, 1]; const second = [4, 1, 7, 6, 2]; const areEqual = (first, second) => {    if(first.length ...

Read More

Strictly increasing sequence JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 1K+ Views

Given a sequence of integers as an array, we have to determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.For example −For sequence = [1, 3, 2, 1], the output should be function(sequence) = false. There is no one element in this array that can be removed in order to get a strictly increasing sequence.For sequence = [1, 3, 2], the output should be function(sequence) = true. You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to ...

Read More

Filter away object in array with null values JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 2K+ Views

Let’s say, we have an array of objects about some employees of a company. But the array contains some bad data i.e., key pointing to empty strings or false values. Our job is to write a function that takes in the array and away the objects that have null or undefined or empty string value for the name key and return the new object.The array of objects are like this −let data = [{    "name": "Ramesh Dhiman",    "age": 67,    "experience": 45,    "description": "" }, {       "name": "",       "age": 31,   ...

Read More

Create a Calculator function in JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 901 Views

We have to write a function, say calculator() that takes in one of the four characters (+, - , *, / ) as the first argument and any number of Number literals after that. Our job is to perform the operation specified as the first argument over those numbers and return the result.If the operation is multiplication or addition, we are required to perform the same operation with every element. But if the operation is subtraction or division, we have to consider the first element as neutral and subtract all other elements from it or divide it by all other ...

Read More

Return the largest array between arrays JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 377 Views

We have an array of arrays that contains some numbers, we have to write a function that returns the takes in that array and returns the index of the subarray that has the maximum sum. If more than one subarray has the same maximum sum, we have to return the index of first such subarray.Therefore, let’s write the code for this −Exampleconst arr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]; const findMaxSubArray = (arr) => {    const add = (array) => array.reduce((acc, val) => acc+val);    return arr.reduce((acc, val, ...

Read More

How to sum all elements in a nested array? JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 2K+ 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

Read More

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

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 514 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
AmitDiwan
Updated on 24-Aug-2020 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
AmitDiwan
Updated on 24-Aug-2020 310 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

Is there any more efficient way to code this &ldquo;2 Sum&rdquo; Questions JavaScript

AmitDiwan
AmitDiwan
Updated on 24-Aug-2020 311 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
Showing 4291–4300 of 5,338 articles
« Prev 1 428 429 430 431 432 534 Next »
Advertisements