Found 6683 Articles for Javascript

Longest subarray with absolute difference equal to some number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:20:56

106 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's say arr, and a number, let's say num, as the second argument. The function should find and return the length of the longest subarray (contiguous or non-contiguous) whose each pair has an absolute difference less than or equal to num.For example, if the input array and the number are −const arr = [7, 9, 8, 6, 6, 3]; const num = 1;Then the output should be −const output = 3, because the desired subarray is [7, 6, 6]ExampleThe code for ... Read More

Finding matching pair from an array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:22:32

1K+ Views

We are required to write a JavaScript function that takes in an array of Integers that might contain some repeating values. Our function should find out the number of pairs of identical integers we can extract out of the array.For example −If the input array is −const arr = [1, 5, 2, 1, 6, 2, 2, 9];Then the output should be −const output = 2;because the desired pairs are 1, 1 and 2, 2ExampleThe code for this will be − Live Democonst arr = [1, 5, 2, 1, 6, 2, 2, 9]; const countPairs = (arr = []) => {   ... Read More

Finding a pair that is divisible by some number in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:29:07

229 Views

We are required to write a JavaScript function that takes in an array of Numbers as the first argument, let's call it arr and a single number as the second argument, let's call it num.The function should find all such pairs from the array where −arr[i] + arr[j] = num, and i < jFor example −If the input array and the number is −const arr = [1, 2, 3, 4, 5, 6]; const num = 4;Then the output should be −const output = [    [1, 3], [2, 6], [3, 5] ];ExampleThe code for this will be − Live Democonst arr ... Read More

Finding desired sum of elements in an array in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:35:34

170 Views

Suppose we have an array of Numbers like this −const arr = [1, 2, 1, 3, 2];We are required to write a JavaScript function that takes in one such array as the first argument. The second argument will be a number that represents a desired sum, let us call it sum, and the third and the last argument will also be a number that represents the count of numbers that should add up to the desired sum from the array (without repetition of elements), let's call this number num.The function should finally return the number of all such groups that ... Read More

Sum excluding one element in JavaScript

AmitDiwan
Updated on 24-Feb-2021 16:38:37

589 Views

Suppose we have an array of Integers like this −const arr = [12, 1, 4, 8, 5];We are required to write a JavaScript function that takes in one such array as the only argument.The function should then return an array of exactly two integers −First integer should be the smallest possible sum of all the array elements excluding any one element.Second integer should be the greatest possible sum of all the array elements excluding any one element.The only condition for us is that we have to do this using one and only one for loop.For example −For the above array, ... Read More

Number pattern in JavaScript

Aayush Mohan Sinha
Updated on 04-Aug-2023 10:05:50

2K+ Views

The comprehension of numerical patterns in JavaScript is indispensable for web developers who aspire to elevate the effectiveness and proficiency of their code. This notion encompasses the creation of a sequence of numbers that abides by a designated principle or arrangement, rendering valuable perception into the fundamental mathematical concepts that regulate the function of complicated algorithms. The capacity to generate numerical patterns is an elemental aptitude for web developers who strive to refine their code and enrich the user experience of their web-based applications. In this write-up, we will scrutinize the complexities of generating numerical patterns in JavaScript, scrutinizing the ... Read More

Positive, negative and zeroes contribution of an array in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:07:36

264 Views

Suppose we have an array of integers, (positive, negative and zero) like this −const arr = [23, -1, 0, 11, 18];We are required to write a JavaScript function that takes in one such array as the first and the only argument. The function should then find the fractional ratio for all three different groups, namely positive, negative and zero.For example −For the above array, its length is 5, the output for this array should be −const output = [.2, .2, .6];The output array will always contain 3 numbers, representing the fractional ratio of negative, zero and positive integers respectively. One ... Read More

Comparing corresponding values of two arrays in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:11:36

296 Views

Suppose we have two array of numbers of the same length like this −const arr1 = [23, 67, 12, 87, 33, 56, 89, 34, 25]; const arr2 = [12, 60, 45, 54, 67, 84, 36, 73, 44];We are required to write a JavaScript function that takes in two such arrays as the first and the second argument. The function should then compare the corresponding values of both the arrays, and the function should return −-1, if the count of corresponding numbers greater in the first array than the second array are more than corresponding numbers greater in the second array1, ... Read More

Converting 12 hour format time to 24 hour format in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:14:50

8K+ Views

We are required to write a JavaScript function that takes in a time string in the following format −const timeStr = '05:00 PM';Note that the string will always be of the same format i.e.HH:MM mmOur function should make some computations on the string received and then return the corresponding 24 hour time in the following format: HH:MMFor example:For the above string, the output should be −const output = '17:00';ExampleThe code for this will be − Live Democonst timeStr = '05:00 PM'; const secondTimeStr = '11:42 PM'; const convertTime = timeStr => {    const [time, modifier] = timeStr.split(' ');    let ... Read More

Removing the odd occurrence of any number/element from an array in JavaScript

AmitDiwan
Updated on 22-Feb-2021 10:17:09

193 Views

Suppose, we have an array of numbers like this −const arr = [1, 6, 3, 1, 3, 1, 6, 3];We are required to write a JavaScript function that takes in one such array as the first and the only argument. Then the function should look for all such numbers in the array that appear for an odd number of times (excluding only once).For example, In the above array, the numbers 1 and 3 both appear for 3 times (odd), so our function should remove the third occurrence of both these numbers.And the output array should look like −const output = ... Read More

Advertisements