Found 6710 Articles for Javascript

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

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

305 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

262 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

1K+ 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

317 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

206 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

Difference between product and sum of digits of a number in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:59:11

282 Views

We are required to write a JavaScript function that takes in an positive integer as the only argument.The function should first count the sum of the digits of the number and then their product. Finally, the function should return the absolute difference of the product and the sum.For example −If the input number is −const num = 12345;Then the output should be −const output = 105;ExampleFollowing is the code −const num = 12345; const product = (num, res = 1) => {    if(num){       return product(Math.floor(num / 10), res * (num % 10));    }    return ... Read More

Pair of similar elements at different indices in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:57:53

107 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 is required to count the number of all such element pairs from the array that are equal in magnitude but are present at different indices.For example −If the input array is −const arr = [7, 9, 5, 7, 7, 5];Then the output should be −const output = 4;because the desired pairs are [7, 7], [7, 7], [7, 7], [5, 5]ExampleFollowing is the code −const arr = [7, 9, 5, 7, 7, 5]; const equalPairCount = (arr = ... Read More

Armstrong number within a range in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:56:08

418 Views

Armstrong Numbers: A positive integer is called an Armstrong number (of order n) if −abcd... = a^n + b^n + c^n + d^n + ...We are required to write a JavaScript function that takes in an array of exactly two numbers specifying a range.The function should return an array of all the Armstrong numbers that falls in that range (including the start and end numbers if they are Armstrong).We will first separately write a function to detect Armstrong numbers and then iterate through the range to fill the array with desired numbers.ExampleFollowing is the code −const range = [11, 1111]; ... Read More

Splitting array of numbers into two arrays with same average in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:54:29

232 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 needs to determine whether there exists a combination of elements of the input array that when they are divided into two groups (may/may not have equal elements), the average of both the groups is just the same. If there exists any such condition the function should return true, false otherwise.For example −If the input array is −const arr = [6, 3, 2, 8, 1, 5, 7, 4];Then the output should be −const output = true;because the combination ... Read More

Similar string groups in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:52:40

408 Views

Two strings str1 and str2 are similar if we can swap two letters (in different positions) of str1, so that it equals str2. Also, two strings str1 and str2 are similar if they are equal.For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar.Formally, each group is such that a word is in ... Read More

Advertisements