Found 6683 Articles for Javascript

Special type of sorting algorithm in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:05:11

58 Views

We are required to write a JavaScript function that takes in an array of integers as the only argument.The function should sort the array based on the following conditions −All even numbers are sorted in increasing orderAll odd numbers are sorted in decreasing orderThe relative positions of the even and odd numbers remain the sameFor example −If the input array is −const arr = [12, 17, 15, 24, 1, 6];Then the output should be −const output = [6, 17, 15, 12, 1, 24];ExampleFollowing is the code −const arr = [12, 17, 15, 24, 1, 6]; const specialSort = (nums = ... Read More

Calculating a number from its factorial in JavaScript

AmitDiwan
Updated on 22-Jan-2021 07:03:19

110 Views

We are required to write a JavaScript function that takes in a number as the only argument.The function should check whether there exists any number whose factorial is the number taken as input.If there exists any such number, we should return that number otherwise we should return -1.For example −If the input is −const num = 720;Then the output should be −const output = 6;ExampleFollowing is the code −const num = 720; const checkForFactorial = num => {    let prod = 1, count = 1;    while(prod

Unique number of occurrences of elements in an array in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:59:06

409 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 whether all the integers that are present in the array appear for unique number of times or not.If they do, the function should return true, false otherwise.For example −If the input array is −const arr = [7, 5, 5, 8, 2, 4, 7];Then the output should be −const output = false;because both the integers 7 and 5 appears for 2 times each.We will first use a hash map to map integers to their frequencies(occurrences) and then ... Read More

Finding a number and its nth multiple in an array in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:56:59

130 Views

We are required to write a JavaScript function that takes in an array of integers as the first argument and a number, say n, as the second argument.The function should check whether there exists two such numbers in the array that one is the nth multiple of the other.If there exists any such pair in the array, the function should return true, false otherwise.For example −If the array and the number are −const arr = [4, 2, 7, 8, 3, 9, 5]; const n = 4;Then the output should be −const output = true;because there exist the numbers 2 and ... Read More

Largest product of n contiguous digits of a number in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:54:53

114 Views

We are required to write a JavaScript function that takes in two numbers as first and the second argument, let us call them m and n.The first number will generally be a number with multiple digits and the second number will always be smaller than the number of digits in the first number.The function should find the group of n consecutive digits from m whose product is the greatest.For example −If the input numbers are −const m = 65467586; const n = 3;Then the output should be −const output = 280;because 7 * 5 * 8 = 280 and it’s ... Read More

Forming and matching strings of an array based on a random string in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:53:25

237 Views

Suppose, we have an array of strings that contains some names like this −const arr = ['Dinesh', 'Mahesh', 'Rohit', 'Kamal', 'Jatin Sapru', 'Jai'];And a random string of characters like this −const str = 'lsoaakjm';We are required to write a JavaScript function that takes in such an array and string as the two argument.Then the function, for each element of the array should check whether that particular element can be formed completely from the string supplied as second argument.If this condition satisfies for any element of the array, we should return that element otherwise we should return an empty string.ExampleFollowing is ... Read More

Smallest number of perfect squares that sums up to n in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:51:08

203 Views

We are required to write a JavaScript function that takes in a positive number, say num, as the only argument.The function should find a combination of such perfect square numbers which when added gives the number provided as input. We have to make that we make use of as less as possible number of perfect squares.For example −If the input number is −const num = 123;Then the output should be −const output = 3;because 123 = 121 + 1 + 1This is a classic Dynamic Programming problem where we can reach the result for a particular number based on the ... Read More

Finding the elements of nth row of Pascal's triangle in JavaScript

AmitDiwan
Updated on 22-Jan-2021 06:49:35

954 Views

Pascal's triangle:Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows.The first few elements of Pascals triangle are −We are required to write a JavaScript function that takes in a positive number, say num as the only argument.The function should return an array of all the elements that must be present in the pascal's triangle in the (num)th row.For example −If the input number is −const num = 9;Then the output should be −const output = [1, 9, 36, 84, 126, 126, 84, 36, 9, 1];ExampleFollowing is the code −const num = 9; const pascalRow = (num) => {    const res = []    while (res.length

Large to Small Sorting Algorithm of already sorted array in JavaScript

AmitDiwan
Updated on 20-Jan-2021 07:03:28

244 Views

Suppose we have an array of integers that is already sorted in the increasing order. We are required to write a JavaScript function that without using the inbuilt Array.prototype.sort() method sorts the array like the following −First number should be the maximumSecond number should be the minimumThird number should be the 2nd maximumFourth number should be the 2nd minimumAnd so on.For example −If the input array is −const arr = [1, 2, 3, 4, 5, 6];Then the output should be −const output = [ 6, 1, 5, 2, 4, 3 ];ExampleFollowing is the code −const arr = [1, 2, 3, ... Read More

Counting substrings of a string that contains only one distinct letter in JavaScript

AmitDiwan
Updated on 20-Jan-2021 07:01:12

155 Views

We are required to write a JavaScript function that takes in a string as the only argument. The task of our function is to count all the contiguous substrings in the input string that contains exactly one distinct letter.The function should then return the count of all such substrings.For example −If the input string is −const str = 'iiiji';Then the output should be −const output = 8;because the desired strings are −'iii', 'i', 'i', 'i', 'i', 'j', 'ii', 'ii'ExampleFollowing is the code −const str = 'iiiji'; const countSpecialStrings = (str = '') => {    let { length } = ... Read More

Advertisements