Found 6683 Articles for Javascript

Finding elements whose successors and predecessors are in array in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:47:10

364 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 construct and return a new array that contains all such elements from the original array whose successor and predecessor are both present in the array. If means, if any element num is in the original array, it should be included in the result array if and only if num - 1 and num + 1 are also present in the array.For example −If the input array is −const arr = [4, 6, 8, 1, 9, 7, ... Read More

Sum of two elements just less than n in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:48:44

117 Views

We 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.The function should then find two such numbers whose sum is greatest in the array but just less than the number num. If there exists no two such numbers whose sum is less than num, our function should return -1.For example −If the input array and the number are −const arr = [34, 75, 33, 23, 1, 24, 54, 8]; const num = 60;Then the output should be −const output = 58;because ... Read More

Compressing string in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:52:06

6K+ Views

We are required to write a JavaScript function that takes in a string that might contain some continuous repeating characters.The function should compress the string like this −'wwwaabbbb' -> 'w3a2b4' 'kkkkj' -> 'k4j'And if the length of the compressed string is greater than or equal to the original string we should return the original string.For example −'aab' can be compressed to 'a2b1' but it increases its length to 4 so our function should return 'aab'ExampleThe code for this will be − Live Democonst str1 = 'wwwaabbbb'; const str2 = 'kkkkj'; const str3 = 'aab'; const compressString = (str = '') => ... Read More

Converting whitespace string to url in JavaScript

AmitDiwan
Updated on 27-Feb-2021 17:53:57

565 Views

In web urls if we provide space in the url, the browser automatically replaces all the spaces with the string '%20'We are required to write a JavaScript function that takes in a string as the first and the only argument. The function should then construct and return a new string in which a whitespace, wherever it was in place, replaced by '%20'For example −If the input string is −const str = 'some extra Space';Then the output should be −const output = 'some%20extra%20%20Space';ExampleThe code for this will be − Live Democonst str = 'some extra Space'; const replaceWhitespace = (str = '') ... Read More

Checking for permutation of a palindrome in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:50:06

563 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The task of our function is to check whether any rearrangement in the characters of the string results into a palindrome string or not. If yes, then our function should return true, false otherwise.For example −If the input string is −const str = 'amadm';Then the output should be −const output = true;because the string can be rearranged to form 'madam' which is a palindrome string.ExampleThe code for this will be − Live Democonst str = 'amadm'; const canFormPalindrome = (str = '') ... Read More

Picking all elements whose value is equal to index in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:53:51

202 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should then construct and return a new array based on the original array.The new array should contain all those elements from the original array whose value was equal to the index they were placed on.Note that we have to check the value and index using 1-based index and not the traditional 0- based index.For example −If the input array is −const arr = [45, 5, 2, 4, 6, 6, 6];Then the output should be −const output = ... Read More

Finding the largest non-repeating number in an array in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:56:04

318 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 then iterate through the array and pick that largest number from the array which only appeared once in the array. After that, return this number and if there is no unique number in the array, we should return -1.We are also told that the maximum value of any array element will not exceed 100 and will be greater than 0 which mean −0 < arr[i] < 101for all i within the array index.For example −If the ... Read More

Checking for uniqueness in an array in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:57:54

117 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument. The function should return true if all the numbers in the array appear only once (i.e., all the numbers are unique), and false otherwise.For example −If the input array is −const arr = [12, 45, 6, 34, 12, 57, 79, 4];Then the output should be −const output = false;because the number 12 appears twice in the array.ExampleThe code for this will be − Live Democonst arr = [12, 45, 6, 34, 12, 57, 79, 4]; const containsAllUnique = (arr ... Read More

Calculating average of a sliding window in JavaScript

AmitDiwan
Updated on 26-Feb-2021 08:59:29

436 Views

We are required to write a JavaScript function that takes in an array of integers, arr, as the first argument and a number, num (num < length of arr) as the second argument. The function should construct and return a new array that contains the average of all possible num contiguous numbers of the array.For example −If the input array and the number are −const arr = [1, 2, 3, 4, 5]; const num = 2;Then the output should be −const output = [1.5, 2.5, 3.5, 4.5];because the possible continuous windows of size two are (1, 2), (2, 3), (3, ... Read More

Finding average of n top marks of each student in JavaScript

AmitDiwan
Updated on 26-Feb-2021 09:00:58

456 Views

Suppose, we have an array of objects that contains information about some students and the marks scored by them over a period of time like this −const marks = [    { id: 231, score: 34 },    { id: 233, score: 37 },    { id: 231, score: 31 },    { id: 233, score: 39 },    { id: 231, score: 44 },    { id: 233, score: 41 },    { id: 231, score: 38 },    { id: 231, score: 31 },    { id: 233, score: 29 },    { id: 231, score: 34 }, ... Read More

Advertisements