Found 6683 Articles for Javascript

Reversing consonants only from a string in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:48:09

332 Views

ProblemWe are required to write a JavaScript function that takes in a string of lowercase english alphabets as the only argument.The function should construct a new string in which the order of consonants is reversed and the vowels hold their relative positions.For example, if the input to the function is −const str = 'somestring';Then the output should be −const output = 'gomenrtiss';ExampleThe code for this will be −const str = 'somestring'; const reverseConsonants = (str = '') => {    const arr = str.split("");    let i = 0, j = arr.length - 1;    const consonants = 'bcdfghjklnpqrstvwxyz';   ... Read More

Finding array intersection and including repeating elements in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:45:33

908 Views

ProblemWe are required to write a JavaScript function that takes in two arrays, arr1 and arr2 as first and second arguments respectively.The function should find the intersection (common elements between both) of the arrays and if there are elements that appear twice in both the arrays, we should include them twice in our result array as well.For example, if the input to the function is −const arr1 = [2, 7, 4, 6, 7, 4]; const arr2 = [7, 1, 9, 7, 4, 5];Then the output should be −const output= [7, 7, 4];ExampleThe code for this will be −const arr1 = ... Read More

Counting n digit Numbers with all unique digits in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:23:20

199 Views

ProblemWe are required to write a JavaScript function that takes in a number, let’s say num, as the only argument. The function should count all such numbers that have num digits and all of their digits are unique.For example, if the input to the function is −const num = 1;Then the output should be −const output = 10;Output Explanation:The numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 all have 1 digit and all are unique.ExampleThe code for this will be − Live Democonst num = 1; const uniqueDigits = (num = 1) => {    const dp = [1, 10];    const sum = [1, 11];    for (let i = 2; i

Placing same string characters apart in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:25:32

93 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters, str, as the first argument and a number, num, (num {    const map = {};    for(let i=0; i{       if(map[a] len){       return "";    };    const res = [];    let index = 0, max = str.length-1;    while(keys.length){       let currKey = keys.shift();       let count = map[currKey];       while(count){          res[index] = currKey;          index = index+2;          if(index>max)             index=1;             count--;       }    }    return res.join(""); }; console.log(placeApart(str));OutputAnd the output in the console will be −mlmklk

Applying f(x) to each array element in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:28:17

79 Views

ProblemSuppose, a mathematical function given by −f(x) = ax2 + bx + cWhere a, b and c are three constants.We are required to write a JavaScript function that takes in a sorted array of Integers, arr as the first argument and a, b and c as second, third and fourth argument. The function should apply the function f(x) to each element of the array arr.And the function should return a sorted version of the transformed array.For example, if the input to the function is −const arr = [-8, -3, -1, 5, 7, 9]; const a = 1; const b = ... Read More

Killing Enemy in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:29:53

207 Views

ProblemSuppose, we have a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero). We are required to write a function that returns the maximum enemies we can kill using only one bomb.The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed.We also have to keep in mind that we can only put the bomb in an empty cell. For example, if the input to the function is −const arr = [ ... Read More

Largest rectangle sum smaller than num in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:31:19

95 Views

ProblemWe are required to write a JavaScript function that takes in a 2-D array of Numbers as the first argument and a target sum number as the second argument.Our function should find out that rectangle from the 2-D array which has the greatest sum among all rectangles in the array but just less than or equal to the target sum specified by the second argument to the function.The function should then finally return that largest sum. For example, if the input to the function is −const arr = [    [1, 0, 1],    [0, -2, 3] ]; const num ... Read More

Finding a number of pairs from arrays with smallest sums in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:32:48

130 Views

ProblemWe are required to write a JavaScript function that takes in two sorted arrays of Integers as the first and the second argument respectively, arr1 and arr2.The third argument to the function will be a number, num, and num will always be lesser than the length of both the arrays. The task of our function is to pick (num) pairs of Integers.Each pair should have its first element from arr1 and second from arr2. The pairs should be picked such that the pairs have the smallest possible sum. Lastly our function should return an array of all these (num) pairs.For ... Read More

Any possible combination to add up to target in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:34:40

290 Views

ProblemWe are required to write a JavaScript function that takes in an array of unique integers, arr, as the first argument, and target sum as the second argument.Our function should count the number of all pairs (with repetition allowed) that can add up to the target sum and return that count.For example, if the input to the function is −const arr = [1, 2, 3]; const target = 4;Then the output should be −const output = 7;Output Explanation:Because, the possible combination ways are −(1, 1, 1, 1) (1, 1, 2) (1, 2, 1) (1, 3) (2, 1, 1) (2, 2) ... Read More

Nth smallest element in sorted 2-D array in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:37:47

299 Views

ProblemSuppose, we have a sorted array of arrays of Numbers (sorted in increasing order) like this −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, 15] ];We are required to write a JavaScript function that takes in in one such array as the first argument and a single Integer, num, as the second argument.Our function is supposed to return the numth smallest element that exists in the array arr.For example, if the input to the function is −const arr = [    [ 1, 5, 9],    [10, 11, 13],    [12, 13, ... Read More

Advertisements