Javascript Articles

Page 378 of 534

Reverse sum array JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 509 Views

We are required to write a function, say reverseSum() that takes in two arrays of Numbers, let’s say first and second and returns a new array that contains, Sum of first element of first array and last element of second array as first element, sum of second element of first array and second last element of second array, and so on.When any of the array runs out of element before the other, we simply push all the remaining elements to the array. Therefore, let's write the code for this function −Exampleconst first = [23, 5, 7, 2, 34, 7, 8]; ...

Read More

Finding next n leap years in JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 612 Views

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts −Part 1: Finding current year via JSThe code to find current year via JS will be −// getting the current year from a new instance of Date object const year = new Date().getFullYear();Part 2: Checking for leap yearWe will now write a function isLeap() that takes in a number and returns a boolean based on the number being a leap year or not.A year is considered as a leap year ...

Read More

Find the longest sub array of consecutive numbers with a while loop in JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 383 Views

We are required to write a function with a while-statement that determines the length of the largest consecutive subarray in an array of positive integers.For instance −If the input array is −const input = [6, 7, 8, 6, 12, 1, 2, 3, 4] --> [1, 2, 3, 4]Then the output should be −4If the input array is −const input = [5, 6, 1, 8, 9, 7] --> [8, 9]Then the output should be −2Therefore, let’s write the code for this function −Exampleconst arr = [6, 7, 8, 6, 12, 1, 2, 3, 4]; const arr1 = [5, 6, 1, 8, ...

Read More

From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 346 Views

Given an array of arrays, each of which contains a set of numbers. We have to write a function that returns an array where each item is the sum of all the items in the corresponding subarray.For example −If the input array is −const numbers = [    [1, 2, 3, 4],    [5, 6, 7],    [8, 9, 10, 11, 12] ];Then output of our function should be −const output = [10, 18, 50];So, let’s write the code for this function −Exampleconst numbers = [    [1, 2, 3, 4],    [5, 6, 7],    [8, 9, 10, 11, ...

Read More

Check whether a number is a Fibonacci number or not JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 495 Views

We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not it comes in the fibonacci series.For example −If the function call is like this −fibonacci(12); fibonacci(89); fibonacci(55); fibonacci(534);Then the output should be −False true true falseNow, let’s write a recursive solution to this problem −Exampleconst fibonacci = (query, count = 1, last = 0) => {    if(count < query){       return fibonacci(query, count+last, count);    };    if(count === query){       return true;    }    return false; }; console.log(fibonacci(12)); console.log(fibonacci(55)); ...

Read More

How to compare two arrays to see how many same elements they have in JavaScript?

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 442 Views

Let’s say, we have two arrays, one contains the correct answer strings of some questions and one contains the answers attempted by a candidate, but somehow the arrays got shuffled and now they don’t have answers in corresponding order. But we can be sure that no two questions had the same answers.Our job now is to write a function that takes these two arrays, checks them for common elements and finds all the common elements between them and then calculates the marks percentage of the candidate based on the count of common answers.Let’s write the code for this function −Exampleconst ...

Read More

Find unique and biggest string values from an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 269 Views

Let’s say, we have an array of objects like this −Exampleconst arr = [    {text:'use'},    {text: 'secur'},    {text: 'form'},    {text: 'user'},    {text: 'users'},    {text: 'form'},    {text: 'secur'},    {text: 'sec'},    {text: 'users'},    {text: 'secu'},    {text: 'secur'},    {text: 'for'},    {text: 'form'} ]Our job is to write a function that takes in this array and a number n and the function should return an array of n objects which have the longest string value for the text key and all the objects should have a unique value for the text ...

Read More

Get n numbers from array starting from given point JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 312 Views

We have to write an array function (Array.prototype.get()) that takes in three arguments first, a number n, second is also a number, say m, (m this.length-1){       return false;    };    const res = [];    for(let i = ind, j = 0; j < num; i += amount, j++){       if(i > this.length-1){          i = i % this.length;       };       if(i < 0){          i = this.length-1;       };       res.push(this[i]);    };    return res; }; console.log(arr.get(4, 6, 'right')); console.log(arr.get(9, 6, 'left'));OutputThe output in the console will be −[ 6, 7, 0, 1 ] [    6, 5, 4, 3, 2,    1, 0, 7, 6 ]

Read More

How to force JavaScript to do math instead of putting two strings together?

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 464 Views

Let’s say, we have an array of strings, basically it is an array of number strings like this −const arr = ['3', '3', '55', '23', '67', '43', '12', '67', '87', '12'];We are required to write a JavaScript function that takes in one such array and returns the sum of all elements of this array instead of concatenating the string to one another.Let’s write the code for this function −Exampleconst arr = ['3', '3', '55', '23', '67', '43', '12', '67', '87', '12']; const sumString = arr => {    const num = arr.reduce((acc, val) => {       const sum ...

Read More

Solve the Sherlock and Array problem in JavaScript

AmitDiwan
AmitDiwan
Updated on 28-Aug-2020 720 Views

Watson gives Sherlock an array A of length N. Then he asks him to determine if there exists an element in the array such that the sum of the elements on its left is equal to the sum of the elements on its right.We have to write this function, it should take in an array of Numbers, and any such number exists in the array, it should return its index, otherwise it should return -1. So, let’s write the code for this function −Exampleconst arr = [1, 2, 3, 4, 5, 7, 3]; const arr2 = [4, 6, 3, 4, ...

Read More
Showing 3771–3780 of 5,338 articles
« Prev 1 376 377 378 379 380 534 Next »
Advertisements