Found 6683 Articles for Javascript

Triplet with desired sum in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:17:35

232 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should prepare and return an array of all such triplets (consecutive or nonconsecutive), that add up to the number specified by the second argument.For example −If the input array and the number are −const arr = [4, 2, 0, 1, 2, 6, 8, 3, 2, 5]; const num = 8;Then the output array should be −const output = [ [ 2, 2, 4 ], [ 1, 3, 4 ], [ 0, 2, ... Read More

Digit sum upto a number of digits of a number in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:15:34

326 Views

We are required to write a JavaScript function that takes in two numbers, let’s say m and n as arguments.n will always be smaller than or equal to the number of digits present in m. The function should calculate and return the sum of first n digits of m.For example −If the input numbers are −const m = 5465767; const n = 4;Then the output should be −const output = 20;because 5 + 4 + 6 + 5 = 20ExampleFollowing is the code −const m = 5465767; const n = 4; const digitSumUpto = (m, n) => {    if(n ... Read More

Smallest prime number just greater than the specified number in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:14:10

309 Views

We are required to write a JavaScript function that takes in a positive integer as the first and the only argument.The function should find one such smallest prime number which is just greater than the number specified as argument.For example −If the input is −const num = 18;Then the output should be:const output = 19;ExampleFollowing is the code:const num = 18; const justGreaterPrime = (num) => {    for (let i = num + 1;; i++) {       let isPrime = true;       for (let d = 2; d * d

Arranging words by their length in a sentence in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:12:28

236 Views

We are required to write a JavaScript function that takes in a sentence as the first and the only argument.A sentence is a special kind of string of characters joined by finite number of whitespaces.The function should rearrange the words of the sentence such that the smallest word (word with least characters) appears first and then followed by bigger ones.For example −If the input string is −const str = 'this is a string';Then the output should be −const output = 'a is this string';ExampleFollowing is the code −const str = 'this is a string'; const arrangeWords = (str = []) ... Read More

Sorting array according to increasing frequency of elements in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:10:22

262 Views

We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most.For example −If the input array is −const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];Then the output array should be −const output = [    3, 2, 1, 9, 9, 4,    4, ... Read More

Shuffling string based on an array in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:08:33

244 Views

We are required to write a JavaScript function that takes in a string, say str as the first argument and an array of positive integers, say arr of the same length as the second argument.Our function should shuffle the characters in the string such that the character at the ith position moves to arr[i] in the shuffled string.For example −If the input string and the array are −const str = 'example'; const arr = [5, 2, 0, 6, 4, 1, 3];Then the output should be −const output = 'alxepem';ExampleFollowing is the code −const str = 'example'; const arr = [5, ... Read More

Finding the longest Substring with At Least n Repeating Characters in JavaScript

AmitDiwan
Updated on 19-Jan-2021 06:07:00

351 Views

We are required to write a JavaScript function that takes in a string as the first argument and a positive integer n as the second argument.The string is likely to contain some repeating characters. The function should find out and return the length of the longest substring from the original string in which all characters appear at least n number of times.For example −If the input string and the number are −const str = 'kdkddj'; const num = 2;Then the output should be −const output = 5;because the desired longest substring is 'kdkdd'ExampleFollowing is the code −const str = 'kdkddj'; ... Read More

Removing smallest subarray to make array sum divisible in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:14:33

78 Views

We are required to write a JavaScript function that takes in an array of positive integers as the first argument and a positive integer as the second argument.The function should figure out and return the length of the smallest subarray that we should delete from the original array in order to make its sum divisible by the number specified by the second argument.For example −If the input is −const arr = [3, 8, 2, 6]; const num = 9;Then the output should be −const output = 2Because the subarray that needs to be deleted is [8, 2]ExampleFollowing is the code ... Read More

Matching odd even indices with values in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:12:03

159 Views

We are required to write a JavaScript function that takes in an array of numbers. The array given as an input to the function have two special properties −The length of the array will always be an even number.The number of even numbers and the number of odd numbers in the array will always be equal (i.e., both being equal to the half of the length of array)The function should shuffle the elements of the array such that all the even values occupy even indices and all the odd values occupy odd indices.Note that there may be more than one ... Read More

Formatting a string to separate identical characters in JavaScript

AmitDiwan
Updated on 18-Jan-2021 05:08:54

92 Views

We are required to write a JavaScript function that takes in a character string as the first and the only argument.The function should try and re-organize the characters present in the string such that no two identical characters are placed adjacent to each other.If there exists at least one such combination then our function should return that combination string otherwise our function should return an empty string.For example −If the input string is −const str = 'add';Then our function can output −const output = 'dad';ExampleFollowing is the code −const str = 'add'; const formatString = (str = '') => { ... Read More

Advertisements