Calculate 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

482 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

Check 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

132 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 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

Check if String is a Combination of Repeated Substrings in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:51:18

296 Views

ProblemWe are required to write a JavaScript function that takes in a string of characters as the only argument. Our function needs to check if the string str can be constructed by taking a substring of it and appending multiple copies of the substring together.For example, if the input to the function is −const str = 'thisthisthisthis';Then the output should be −const output = true;Output Explanation:Because the string is made by appending ‘this’ string repeatedly.ExampleThe code for this will be − Live Democonst str = 'thisthisthisthis'; const repeatedSubstring = (str = '') => {    const {length} = str;    const ... Read More

Sorting String Characters by Frequency in JavaScript

AmitDiwan
Updated on 18-Mar-2021 13:49:54

809 Views

ProblemWe are required to write a JavaScript function that takes in the string of characters as the only argument.Our function should prepare and a new string based on the original string in which the characters that appear for most number of times are placed first followed by number with decreasing frequencies.For example, if the input to the function is −const str = 'free';Then the output should be −const output = 'eefr';Output Explanation:Since e appears twice it is placed first followed by r and f.ExampleThe code for this will be − Live Democonst str = 'free'; const frequencySort = (str = '') ... Read More

Finding Sum of Left Leaves of a BST in JavaScript

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

341 Views

ProblemWe are required to write a JavaScript function that takes in the root of a Binary Search Tree as the only argument.The function should simply calculate the sum of data stored in the left leaves of the BST.For example, if the Tree looks like this −8 / \ 1 10 / \ 5 17Then the output should be −const output = 6;Output Explanation:Because there are two left leaves in the Tree with values 1 and 5.ExampleThe code for this will be − Live Democlass Node{    constructor(data) {       this.data = data;       this.left = null;   ... Read More

Smallest Number After Removing N Digits in JavaScript

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

305 Views

ProblemWe are required to write a JavaScript function that takes in two numbers, let’s call them m and n as first and the second argument respectively.The task of our function is to remove n digits from the number m so that the number m is the smallest possible number after removing n digits. And finally, the function should return the number m after removing digits.For example, if the input to the function is −const m = '45456757'; const n = 3;Then the output should be −const output = '44557';Output Explanation:We removed 5, 6 and 7 digit to get the smallest ... Read More

Advertisements