Sum of Common Elements in Arrays using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:43:56

293 Views

ProblemWe are required to write a JavaScript function that takes in three arrays of numbers. Our function should return the sum of all those numbers that are common in all three arrays.ExampleFollowing is the code − Live Democonst arr1 = [4, 4, 5, 8, 3]; const arr2 = [7, 3, 7, 4, 1]; const arr3 = [11, 0, 7, 3, 4]; const sumCommon = (arr1 = [], arr2 = [], arr3 = []) => {    let sum = 0;    for(let i = 0; i < arr1.length; i++){       const el = arr1[i];       const ind2 = arr2.indexOf(el);       const ind3 = arr3.indexOf(el);       if(ind2 !== -1 && ind3 !== -1){          arr2.splice(ind2, 1);          arr3.splice(ind3, 1);          sum += el;       };    };    return sum; }; console.log(sumCommon(arr1, arr2, arr3));Output7

Greatest Number Divisible by N Within a Bound in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:50:05

386 Views

ProblemWe are required to write a JavaScript function that takes in a number n and bound number b.Our function should find the largest integer num, such that −num is divisible by divisornum is less than or equal to boundnum is greater than 0.ExampleFollowing is the code − Live Democonst n = 14; const b = 400; const biggestDivisible = (n, b) => {    let max = 0;    for(let j = n; j max){          max = j;       };    }    return max; }; console.log(biggestDivisible(n, b));Output392

Sum of Minimum Values in Each Row of a 2-D Array Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:49:45

322 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of numbers. Our function should pick the smallest number from each row of the 2-D array and then finally return the sum of those smallest numbers.ExampleFollowing is the code − Live Democonst arr = [    [2, 5, 1, 6],    [6, 8, 5, 8],    [3, 6, 7, 5],    [9, 11, 13, 12] ]; const sumSmallest = (arr = []) => {    const findSmallest = array => array.reduce((acc, val) => {       return Math.min(acc, val);    }, Infinity)    let sum = 0;    arr.forEach(sub => {       sum += findSmallest(sub);    });    return sum; }; console.log(sumSmallest(arr));Output18

Find the Only Out-of-Sequence Number in an Array Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:48:34

477 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. The array is sorted in ascending / increasing order and only one element in the array is out of order.Our function should find and return that element.ExampleFollowing is the code −const arr = [1, 2, 3, 4, 17, 5, 6, 7, 8]; const findWrongNumber = (arr = []) > {    for(let i = 0; i < arr.length - 1; i++){       const el = arr[i];       if(el - arr[i + 1] < 0 && arr[i + 1] - arr[i + 2] > 0){          return arr[i + 1];       }    }; }; console.log(findWrongNumber(arr));Output17

Count Letters in Their Alphabetical Positions for Array of Strings Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:48:14

312 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings of english lowercase alphabets.Our function should map the input array to an array whose corresponding elements are the count of the number of characters that had the same 1-based index in the index as their 1-based index in the alphabets.For instance−This count for the string ‘akcle’ will be 3 because the characters ‘a’, ‘c’ and ‘e’ have 1-based index of 1, 3 and 5 respectively both in the string and the english alphabets.ExampleFollowing is the code − Live Democonst arr = ["abode", "ABc", "xyzD"]; const findIndexPairCount ... Read More

Break String into Chunks of Defined Length and Remove Spaces Using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:47:45

270 Views

ProblemWe are required to write a JavaScript function that takes in a string sentence that might contain spaces as the first argument and a number as the second argument.Our function should first remove all the spaces from the string and then break the string into a number of chunks specified by the second argument.All the string chunks should have the same length with an exception of last chunk, which might, in some cases, have a different length.ExampleFollowing is the code − Live Democonst num = 5; const str = 'This is an example string'; const splitString = (str = '', num ... Read More

Count Numbers Divisible by a Given Number in a Range using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:45:22

362 Views

ProblemWe are required to write a JavaScript function that takes in a range of two integers as the first argument and a number as the second argument.Our function should find all the numbers divisible by the input number in the specified range and return their count.ExampleFollowing is the code − Live Democonst range = [6, 57]; const num = 3; const findDivisibleCount = (num = 1, [l, h]) => {    let count = 0;    for(let i = l; i

Construct Array of First N Multiples of an Input Number in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:44:45

381 Views

ProblemWe are required to write a JavaScript function that takes in two numbers, let say m and n.Our function should construct and return an array of first n natural multiples of m.ExampleFollowing is the code − Live Democonst m = 6; const n = 14; const firstNMultiple = (m = 1, n = 1) => {    const res = [];    for(let i = 1; i

Find Character with Longest Consecutive Repetitions in a String using JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:44:26

931 Views

ProblemWe are required to write a JavaScript function that takes in a string. Our function should return an array of exactly two elements the first element will be characters that makes the most number of consecutive appearances in the string and second will be its number of appearances.ExampleFollowing is the code − Live Democonst str = 'tdfdffddffsdsfffffsdsdsddddd'; const findConsecutiveCount = (str = '') => {    let res='';    let count=1;    let arr = []    for (let i=0;iv

Return Last N Even Numbers from Input Array in JavaScript

AmitDiwan
Updated on 20-Apr-2021 09:43:44

180 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument.Our function should pick and return an array of last n even numbers present in the input array.ExampleFollowing is the code − Live Democonst arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const num = 3; const pickEvens = (arr = [], num = 1) => {    const res = [];    for(let index = arr.length - 1; index >= 0; index -= 1){       if (res.length === num){          break;       };       const number = arr[index];       if (number % 2 === 0){          res.unshift(number);       };    };    return res; }; console.log(pickEvens(arr, num));Output[4, 6, 8]

Advertisements