Found 6710 Articles for Javascript

Limiting duplicate character occurrence to once in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:42:10

184 Views

ProblemWe are required to write a JavaScript function that takes in a string, str, as the only argument.The function should prepare a new string based on the input string in which the only one appearance of each character is kept and the character kept is the one which makes the result string lexicographically the smallest.For example, if the input to the function is −const str = 'cbacdcbc';Then the output should be −const output = 'acdb';Output Explanation:Note that we could have removed any occurrence of ‘c’ from the string but we removed the very first, which makes the string lexicographically the ... Read More

Maximum length product of unique words in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:39:40

138 Views

ProblemWe are required to write a JavaScript function that takes in an array of strings (only lowercase string alphabets) as the first and the only argument.The function should pick two such strings from the array that shares no common characters and have the maximum product of their lengths. And then our function should return the length product of two such strings. If there exist no such strings in the array, we should return 0.For example, if the input to the function is −const arr = ["karl", "n", "the", "car", "mint", "alpha"];Then the output should be −const output = 20;Output Explanation:The ... Read More

Switching on and off bulb in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:37:36

661 Views

ProblemConsider this following situation −There are n bulbs that are initially off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on).In general, for the ith round, we toggle every i bulb. And lastly for the nth round, we only toggle the last bulb.We are required to write a JavaScript function that takes n as the only input and finds out how many bulbs are on after n rounds.For example, if the input to the function ... Read More

Finding maximum number from two arrays in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:35:27

644 Views

ProblemWe are required to write a JavaScript function that takes in two arrays of single digit numbers representing two numbers, arr1 and arr2 as the first and second argument. The third argument to the function will be a number, num (num {    const map = new Map();    const match = (a, b, num) => {       if (map.has(a + ', ' + b + ', ' + num)) {          return map.get(a + ', ' + b + ', ' + num);       }       let output = ... Read More

Summing up to amount with fewest coins in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:31:46

179 Views

ProblemWe are required to write a JavaScript function that takes in arr, arr, as the first argument. This array basically specifies the different types of coin denominations we have.The second argument to the function is a number, amount, which specifies the amount we want to add up to. Our function should simply return the minimum number of coins required to add up to that amount.If we can, in no way, reach amount, we should return -1.For example, if the input to the function is −const arr = [1, 2, 5]; const amount = 17;Then the output should be −const output ... Read More

Uneven sorting of array in JavaScript

AmitDiwan
Updated on 20-Mar-2021 05:29:41

206 Views

ProblemWe are required to write a JavaScript function that takes in an array of numbers, arr, as the only argument. Our function should sort this array in such a way that after sorting, the elements should follow this pattern −arr[0] < arr[1] > arr[2] < arr[3]....For example, if the input to the function is −const arr = [1, 5, 1, 1, 6, 4];Then the output can (there can be more than one possible answer as well) be −const output = [2, 3, 1, 3, 1, 2];ExampleThe code for this will be −const arr = [1, 5, 1, 1, 6, 4]; ... Read More

Counting pairs with range sum in a limit in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:17:22

282 Views

Range SumRange sum rangeSum(i, j) is defined as the sum of the elements in an array between indices i and j (i ≤ j), inclusive.ProblemWe are required to write a JavaScript function that takes in an array of Integers, arr, as the first argument and two numbers, upper and lower as the second and third element.Our function is supposed to return the number of range sums that lie between the range [upper, lower], (both inclusive).For example, if the input to the function is −const arr = [1, 4, 3]; const upper = 5; const lower = 2;Then the output should ... Read More

Longest path in 2-D that contains increasing sequence in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:15:12

322 Views

Increasing SequenceA sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence.For instance, 4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequenceProblem:We are required to write a JavaScript function that takes in a 2-D array of numbers, arr, as the only argument. Our function should find and return the length of that longest path in the array that contains only increasing numbers.For example, if the input to the function is −const arr = [    [4, 5, ... Read More

Adding elements to array to make its sum diverse in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:10:49

126 Views

ProblemWe are required to write a JavaScript function that takes in an array of Numbers, arr, as the first argument and a single number, num, as the second argument.We should, by adding elements to it, make our array such that any sum can be obtained by adding specific numbers from it between [0, num] (including both). Our function should finally return the minimum number of numbers required to add to the array so that it can produce any sum between 0 and num.For example, if the input to the function is −const arr = [1, 5, 10]; const sum = ... Read More

Checking for increasing triplet in JavaScript

AmitDiwan
Updated on 19-Mar-2021 06:07:44

266 Views

Increasing Numbers:A sequence of numbers in which each succeeding element is either greater or equal to the preceding element is an increasing sequence.For instance, 4, 6, 8, 9, 11, 14 is increasing sequence 3, 3, 3, 3, 3, 3, 3 is also an increasing sequenceProblemWe are required to write a JavaScript function that takes in an array of Numbers, arr, as the only argument. The function should check whether there exist three consecutive elements in the array that are increasing.For example, if the input to the function is −const arr = [4, 1, 5, 7, 3, 1, 4];Then the output ... Read More

Advertisements