Found 6710 Articles for Javascript

Sum of All Possible Odd Length Subarrays in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:21:49

395 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should first permute all possible subarrays from the original array that have an odd length. And then the function should find the combined sum of all the elements of those subarrays and return the sum.For example −If the input array is −const arr = [1, 2, 3];Then the output should be −const output = 12;because the desired subarrays are [1], [2], [3], [1, 2, 3]ExampleFollowing is the code −const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3, ... Read More

Subarray pairs with equal sums in JavaScript

AmitDiwan
Updated on 27-Jan-2021 06:19:52

149 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should determine whether there exists any way in which we can split the array into two subarray such that the sum of the elements present in the two subarrays are equal. While dividing the elements into subarrays we have to make sure that no element from the original array is left.For example −If the input array is −const arr = [5, 3, 7, 4, 1, 8, 2, 6];Then the output should be −const output = true;because the desired subarrays are: ... Read More

Largest Substring Between Two Equal Characters in a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:55:06

202 Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should find that longest string which is sandwiched between two identical characters and return its length.For example −If the input string is −const str = 'sadtrsewak';Then the output should be −const output = 6;because between two 'a' we have the longest desired substring of length 6.ExampleFollowing is the code −const str = 'sadtrsewak'; const longestSubstringBetween = (str = '') => {    const map = {};    let res = -1;    for(let i = 0; i < str.length; i++){ ... Read More

Checking for uniqueness of a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:51:19

191 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should return true if all the characters present in the string are unique. And if even one character appears for more than one, the function should return false.We will use a hash set to keep track of the characters we encounter in the string and if at any stage of iteration, we encounter duplicate characters, we will return false otherwise at the end of iteration, we will return true.ExampleFollowing is the code −const str = 'abschyie'; const checkUniqueness ... Read More

Maximum Product of Two Numbers in a List of Integers in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:48:39

270 Views

We are required to write a JavaScript function that takes in an array of integers as the first and the only argument.The function should find the maximum product that can be achieved multiplying any two elements of the array. The condition for us is that we have to do this in linear time and constant space.For example −If the input array is −const arr = [3, 9, 2, 1, 0];Then the output should be −const output = 27;because it’s the greatest product and can be achieved by multiplying 3 and 9.ExampleFollowing is the code −const arr = [3, 9, 2, ... Read More

Check if a string is repeating in itself in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:44:45

821 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should detect if the string is a repetition of a same set of characters or not.If it is a repetition of the same set of characters then we should return true, false otherwise.For example −If the input string is −const str = 'carcarcarcar';Then the output should be −const output = true;because the string 'car' is getting repeated over and over again in the string.ExampleFollowing is the code −const str = 'carcarcarcar'; const isRepeating = (str = '') => { ... Read More

Counting the number of palindromes that can be constructed from a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:42:58

341 Views

We are required to write a JavaScript function that takes in a string of characters as the first argument, say str, and a number, say num, as the second argument.The function should count the number of palindrome strings all exactly of length num can be constructed from the given string str. The function should then finally return the count.For example −If the input string and the number is −const str = 'ij'; const num = 4;Then the output should be −const output = 4;because those four possible palindrome strings are −'iiii', 'jjjj', 'ijji', 'jiij'Approach:We will first count the number of ... Read More

In-place Algorithm to Move Zeros to End of List in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 09:55:30

951 Views

Effectively manipulating data structures is a crucial aspect of contemporary software development. In JavaScript, one typical task is relocating all the zeros in an array to the end, while preserving the order of the non-zero elements. Although various methods exist to achieve this, the in-situ algorithm is a particularly potent technique that avoids the necessity of extra data structures. In this discourse, we will scrutinize the complexities of the in-situ algorithm to move zeros to the end of an array in JavaScript, probing the underlying reasoning and offering systematic instructions for execution. By mastering this technique, software developers can refine ... Read More

Returning acronym based on a string in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:38:41

1K+ Views

We are required to write a JavaScript function that takes in a string of characters as the only argument.The function should build and return the acronym based on the string phrase provided as input.While constructing the acronym the function should take only those words into consideration that starts with an uppercase letter.For example −If the input string is −const str = 'Polar Satellite Launch Vehicle';Then the output should be −const output = 'PSLV';ExampleFollowing is the code −const str = 'Polar Satellite Launch Vehicle'; const buildAcronym = (str = '') => {    const strArr = str.split(' ');    let res ... Read More

Counting largest numbers in row and column in 2-D array in JavaScript

AmitDiwan
Updated on 23-Jan-2021 06:36:48

432 Views

We are required to write a JavaScript function that takes in a two-dimensional array of integers as the only argument.The task of our function is to calculate the count of all such integers from the array that are the greatest both within their row and the column.The function should then return that count.For example −If the input array is −const arr = [    [21, 23, 22],    [26, 26, 25],    [21, 25, 27] ];Then the output should be −const output = 3;because those three numbers are 26, 26, 27ExampleFollowing is the code −const arr = [    [21, ... Read More

Advertisements