Found 6710 Articles for Javascript

Sorting numbers in descending order but with `0`s at the start JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:04:15

494 Views

We are required to write a JavaScript function that takes in an array of numbers. The function should sort the array of numbers on the following criteria −---If the array contains any zeros, they should all appear in the beginning.---All the remaining numbers should be placed in a decreasing order.For example −If the input array is −const arr = [4, 7, 0 ,3, 5, 1, 0];Then after applying the sort, the array should become −const output = [0, 0, 7, 5, 4, 3, 1];We will use the Array.prototype.sort() method here.For the decreasing order sort, we will take the difference of ... Read More

Calculating the sum of digits of factorial JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:03:19

495 Views

We are required to write a JavaScript function that takes in a number. The function should first calculate the factorial of that number and then it should return the sum of the digits of the calculated factorial.For example −For the number 6, the factorial will be 720, so the output should be 9Exampleconst factorial = (num) => {    if (num == 1) return 1;    return num * factorial(num-1); }; const sumOfDigits = (num = 1) => {    const str = num.toString();    let sum = 0;    for (var x = -1; ++x < str.length;) {       sum += +str[x];    };    return sum; }; const sumFactorialDigits = num => sumOfDigits(factorial(num)); console.log(sumFactorialDigits(6));OutputThis will produce the following output −9

Number of letters in the counting JavaScript

AmitDiwan
Updated on 25-Nov-2020 05:01:55

363 Views

We are required to write a JavaScript function that takes in a number, say n. The function should count the letters in the number names from 1 to n.For example − If n = 5;Then the numbers are one, two, three, four, five.And the total number of letters are 19, so the output should be 19.Exampleconst sumUpto = (num = 1) => {    let sum = 0;    const lenHundred = 7;    const lenThousand = 8;    const lenPlaceOnes = [0,3,3,5,4,4,3,5,5,4];    const lenPlaceTens = [0,3,6,6,5,5,5,7,6,6];    for (let i = 1; i 100 && i % 100 != 0) {          sum += 3;       }    }    return sum; } console.log(sumUpto(12)); console.log(sumUpto(5)); console.log(sumUpto(122));OutputThis will produce the following output −51 19 1280

Modify an array based on another array JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:57:14

940 Views

Suppose, we have a reference array of phrases like this −const reference = ["your", "majesty", "they", "are", "ready"];And we are required to join some of the elements of above array based on another array so if another array is this −const another = ["your", "they are"];The result would be like −result = ["your", "majesty", "they are", "ready"];Here, we compared the elements in both the arrays, we joined the elements of the first array if they existed together in the second array.We are required to write a JavaScript function that takes in two such arrays and returns a new joined array.Exampleconst ... Read More

Fuzzy Search Algorithm in JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:55:12

1K+ Views

We are required to write a JavaScript String function that takes in a search string can loosely checks for the search string in the string it is used with.The function should take this criterion into consideration: It should loop through the search query letters and check if they occur in the same order in the string.For example −('a haystack with a needle').fuzzySearch('hay sucks'); // false ('a haystack with a needle').fuzzySearch('sack hand'); // trueExampleconst fuzzySearch = function (query) {    const str = this.toLowerCase();    let i = 0, n = -1, l;    query = query.toLowerCase();    for (; l ... Read More

Longest decreasing subsequence subarray in JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:53:54

217 Views

We are required to write a JavaScript function that takes in an array of Integers. The function should return the length of the longest decreasing subsequence from the array.For example −If the input array is −const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7];Then the output should be −const output = 4;because the longest decreasing subsequence (of consecutive words) is [5, 4, 3, 2];Exampleconst arr = [5, 2, 5, 4, 3, 2, 4, 6, 7]; const decreasingSequence = (arr = []) => {    let longest = [];    let curr = [];    const setDefault = ... Read More

Finding all possible combined (plus and minus) sums of n arguments JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:52:35

340 Views

We are required to write a JavaScript function that in any number of arguments (all of Number type).The function should calculate all possible sums of addition and subtraction.For example − If the arguments are 1, 2, 3Then all possible combinations are −1 + 2 + 3 1 - 2 - 3 1 + 2 - 3 1 - 2 + 3Finally, the function should the sum that is closest to 0. In this case, that answer would just be 0.Exampleconst findSmallestPositive = (...arr) => {    let set = new Set([Math.abs(arr[0])]);    for (let i = 1;    i < ... Read More

Find first duplicate item in array in linear time JavaScript

AmitDiwan
Updated on 25-Nov-2020 04:51:22

305 Views

We are required to write a JavaScript function that takes in a read only array of n + 1 integers between 1 and n.The function should find one number that repeats in linear time and using at most O(n) space.For example If the input array is −const arr = [3 4 1 4 1];Then the output should be −const output = 1;If there are multiple possible answers ( like above ), we should output any one. If there is no duplicate, we should output -1.Exampleconst arr = [3, 4, 1, 4, 1]; const findRepeatedNumber = (arr = []) => { ... Read More

JavaScript Quicksort Recursive

Alshifa Hasnain
Updated on 17-Jan-2025 19:47:24

1K+ Views

In this article, we will learn to implement Quicksort recursively in JavaScript. Quicksort is one of the most efficient and widely used sorting algorithms, known for its divide-and-conquer approach. What is Quicksort? Quicksort is a divide-and-conquer sorting algorithm that sorts an array by selecting a pivot element and partitioning the array into two subarrays: elements smaller than the pivot and elements greater than or equal to the pivot. It then recursively sorts the subarrays.  Algorithm Quicksort follows the below steps − Step 1 − Make any element the pivot (preferably first or last, ... Read More

JavaScript - summing numbers from strings nested in array

AmitDiwan
Updated on 24-Nov-2020 11:06:51

183 Views

Suppose, we have an array that contains some demo credit card numbers like this −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260'];We have been tasked with creating a function that takes in this array. The function must return the credit card number with the greatest sum of digits.If two credit card numbers have the same sum, then the last credit card number should be returned by the function.ExampleThe code for this will be −const arr = ['4916-2600-1804-0530', '4779-252888-3972', '4252-278893-7978', '4556-4242-9283-2260']; const findGreatestNumber = (arr) => {    let n, i = 0, sums;    sums = [];    while (i < ... Read More

Advertisements