Found 6710 Articles for Javascript

Joining strings to form palindrome pairs in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:02:16

265 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings as the only argument. The function is supposed to return an array of arrays of all the index pairs joining the strings at which yields a new palindrome string.For example, if the input to the function is −const arr = ['tab', 'cat', 'bat'];Then the output should be −const output = [[0, 2], [2, 0]];Output Explanation:Because both the strings ‘battab’ and ‘tabbat’ are palindromes.ExampleThe code for this will be −const arr = ['tab', 'cat', 'bat']; const isPalindrome = (str = '') => {    let i ... Read More

Calculating 1s in binary representation of numbers in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:59:46

461 Views

ProblemWe are required to write a JavaScript function that takes in a single Integer, num, as the first and the only argument. Our function should prepare an array for every number between 0 and num (including both of them), for each number, the corresponding element should be the number of 1s contained in the binary representation of that number.For example, if the input to the function is −const num = 4;Then the output should be −const output = [0, 1, 1, 2, 1];Output Explanation:Because 0 contains 0 1s in its binary form 1 contains 1, and so on.ExampleThe code for ... Read More

Weight sum of a nested array in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:55:59

484 Views

ProblemWe are required to write a JavaScript function that takes in a nested array, arr (nested up to any level) as the only argument.The function should calculate the weighted sum of the nested array and return that sum.For calculating nested sum, we multiply a specific element with its level of nesting and add throughout the array.For example, if the input to the function is −const arr = [4, 7, [6, 1, [5, 2]]];Then the output should be −const output = 46;Output Explanation:The sum will be calculated like this −(4 * 1) + ( 7 * 1) + (6 * 2) ... Read More

Checking if a number is a valid power of 4 in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:54:14

352 Views

ProblemWe are required to write a JavaScript function that takes in a single integer, num, as the only argument. Our function should check whether this number is a valid power of 4 or not. If it is a power of 4, we should return true, false otherwise.For example, if the input to the function is −const num1 = 2356; const num2 = 16;Then the output should be −const output1 = false; const output2 = true;ExampleThe code for this will be −const num1 = 2356; const num2 = 16; const isPowerOfFour = (num = 1) => {    let bool = ... Read More

Breaking integer to maximize product in JavaScript

AmitDiwan
Updated on 19-Mar-2021 05:51:28

133 Views

ProblemWe are required to write a JavaScript function that takes in an Integer, num, as the first and the only argument.Our function should break these integers into at least two chunks which when added gives the sum integer num and when multiplied gives maximum possible product. Finally, our function should return this maximum possible product.For example, if the input to the function is −const num = 10;Then the output should be −const output = 36;Output Explanation:Because 10 can be broken into 3 + 3 + 4 which when multiplied gives 36.ExampleThe code for this will be −const num = 10; ... Read More

Reversing consonants only from a string in JavaScript

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

389 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

1K+ 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

288 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

161 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

143 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

Advertisements