Found 6710 Articles for Javascript

Finding the nth power of array element present at nth index using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:46:31

322 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers. Our function should map the input array to another array in which each element is raised to its 0-based index.And finally, our function should return this new array.ExampleFollowing is the code − Live Democonst arr = [5, 2, 3, 7, 6, 2]; const findNthPower = (arr = []) => {    const res = [];    for(let i = 0; i < arr.length; i++){       const el = arr[i];       const curr = Math.pow(el, i);       res[i] = curr;    };    return res; }; console.log(findNthPower(arr));Output[ 1, 2, 9, 343, 1296, 32 ]

Evaluating a mathematical expression considering Operator Precedence in JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:46:11

530 Views

ProblemWe are required to write a JavaScript function that takes in a mathematical expression as a string and return its result as a number.We need to support the following mathematical operators −Division / (as floating-point division)Addition +Subtraction -Multiplication *Operators are always evaluated from left-to-right, and * and / must be evaluated before + and -.ExampleFollowing is the code − Live Democonst exp = '6 - 4'; const findResult = (exp = '') => {    const digits = '0123456789.';    const operators = ['+', '-', '*', '/', 'negate'];    const legend = {       '+': { pred: 2, func: ... Read More

Deleting occurrences of an element if it occurs more than n times using JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:26:25

296 Views

In the realm of JavaScript programming, effectively managing the occurrences of elements within an array is of paramount importance. Specifically, the ability to delete instances of an element if it surpasses a certain threshold, denoted by the variable "n, " can greatly enhance the efficiency and accuracy of data manipulation tasks. By harnessing the power of JavaScript, developers can employ a robust approach to selectively remove redundant element occurrences from arrays. In this article, we will embark on a comprehensive exploration of the step-by-step process for deleting occurrences of an element if it occurs more than "n" times using JavaScript, ... Read More

Sorting 2-D array of strings and finding the diagonal element using JavaScript

AmitDiwan
Updated on 21-Apr-2021 06:44:33

156 Views

ProblemWe are required to write a JavaScript function that takes in an array of n strings. And each string in the array consists of exactly n characters.Our function should first sort the array in alphabetical order. And then return the string formed by the characters present at the principal diagonal starting from the top left corner.ExampleFollowing is the code − Live Democonst arr = [    'star',    'abcd',    'calm',    'need' ]; const sortPickDiagonal = () => {    const copy = arr.slice();    copy.sort();    let res = '';    for(let i = 0; i < copy.length; i++){ ... Read More

Finding the sum of all common elements within arrays using JavaScript

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

286 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

375 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

Finding the sum of minimum value in each row of a 2-D array using JavaScript

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

315 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

Finding the only out of sequence number from an array using JavaScript

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

467 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

Counting the number of letters that occupy their positions in the alphabets for array of strings using JavaScript

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

306 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

Breaking a string into chunks of defined length and removing spaces using JavaScript

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

259 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

Advertisements