Round Up to the Nearest N in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:13:19

2K+ Views

Suppose we have a number, const num = 76;However, If we round off this number to nearest 10 place, the result will be 80If we round off this number to nearest 100 place, the result will be 100If we round off this number to nearest 1000 place, the result will be 0We are required to write a JavaScript function that takes in a number to be rounded as the first argument and the rounding off factor as the second argument.The function should return the result after rounding off the number.ExampleThe code for this will be −const num = 76; const ... Read More

Find Specific Key-Value in Array of Objects Using JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:12:04

3K+ Views

Suppose we have a JSON object like this −const obj = {    "LAPTOP": [{       "productId": "123"    }],    "DESKTOP": [{       "productId": "456"    }],    "MOUSE": [{       "productId": "789"    }, {       "productId": "012"    }],    "KEY-BOARD": [{       "productId": "345"    }] };We are required to write a JavaScript function that takes in one such object as the first argument, and a key value pair as the second argument.The key value pair is basically nothing but an object like this −const pair ... Read More

Generating Random Prime Number in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:10:36

644 Views

We are required to write a JavaScript function that takes in two numbers specifying a range. Our function should return a random prime number that falls in that rangeExampleThe code for this will be −const range = [100, 1000]; const getPrimes = (min, max) => {    const result = Array(max + 1)    .fill(0)    .map((_, i) => i);    for (let i = 2; i {    return Math.floor(Math.random() * (max − min + 1) + min); }; const getRandomPrime = ([min, max]) => {    const primes = getPrimes(min, max);    return primes[getRandomNum(0, primes.length − 1)]; }; console.log(getRandomPrime(range));OutputAnd the output in the console will be −311Output is likely to differ in each run.

Iterate Through Array Adding Occurrences of True in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:10:34

160 Views

Suppose we have an array of true/false represented by 't'/'f' which we retrieved from some database like this −const arr = ['f', 't', 'f', 't', 't', 't', 'f', 'f', 't', 't', 't', 't', 't', 't', 'f', 't'];We are required to write a JavaScript function that takes in one such array. Our function should count the consecutive appearances of those 't' that are sandwiched between two 'f's and return an array of that count.Therefore, for the above array, the output should look like −const output = [1, 3, 6, 1];ExampleThe code for this will be −const arr = ['f', 't', 'f', ... Read More

Count Occurrence of a Specific String in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:09:41

234 Views

We are required to write a JavaScript function that takes in two strings, say str1 and str2. The function should then count and return the number of times str2 appears in str1'For example −count('this is a string', 'is') should return 2;ExampleThe code for this will be −const str1 = 'this is a string'; const str2 = 'is'; const countOccurrences = (str1, str2, allowOverlapping = true) => {    str1 += "";    str2 += "";    if (str2.length = 0) {          ++n;          pos += step;       } else break;    }    return n; }; console.log(countOccurrences(str1, str2));OutputAnd the output in the console will be −2

Group By JavaScript Array Object

AmitDiwan
Updated on 21-Nov-2020 10:09:30

2K+ Views

Suppose we have an array of arrays that contains the marks of some students in some subjects like this −const arr = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ];We are required to write a JavaScript function that takes in one such array and returns an object of objects.The return object should contain an object for each unique subject, and that object should contain information like the number of appearances of that language, sum of total marks and the average.ExampleThe code for this will be −const arr = [    ["English", 52], ... Read More

Generate Random String with Specific Length in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:07:49

998 Views

We are required to write a JavaScript function that takes in a number as one and the only argument. The function should then return a randomly generated string of the length specified by the argument.The character set to be used for the string generation should only contain uppercase and lowercase alphabets (no whitespaces, punctuations or numerals).ExampleThe code for this will be −const num = 13; const randomString = (len = 1) => {    const charSet =    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';    let randomString = '';    for (let i = 0; i < len; i++) {       let randomPoz ... Read More

Compute Cartesian Product of Elements in an Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:07:40

1K+ Views

Cartesian ProductThe Cartesian product of two sets (arrays) A and B, denoted A × B, is the set (array) of all ordered pairs (a, b) where a is in A and b is in B.In simpler terms, a cartesian product of two arrays is a permutation of all possible arrays of two elements whose first element belongs to the first array and the second element belongs to the second array.For example − If the two arrays are −const arr1 = [1, 2, 3]; const arr2 = [4, 5];Then their cartesian product will be −const product = [[1, 4], [1, 5], ... Read More

Sorting Array of Strings Having Year and Month in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:07:03

264 Views

Suppose, we have an array of Strings that contains month-year combined strings like this −const arr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"];We are required to write a JavaScript function that takes in one such array and sorts these dates in oldest to latest order.ExampleThe code for this will be −const arr = ["2009-feb", "2009-jan", "2010-mar", "2010-jan", "2011-jul", "2011-sep", "2011-jan", "2012-jan", "2012-dec", "2012-feb", "2013-may", "2013-jul", "2013-jun", "2014-jan", "2014-dec", "2014-may", "2015-may", "2015-jan", "2015-jun", "2016-jan", "2016-dec"]; const sorter = (a, b) => {    const getDate = ... Read More

Find All Partitions of a Multiset with Distinct Elements in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:06:33

245 Views

Let's say we have such an array −const arr = [A, A, B, B, C, C, D, E];We are required to create an algorithm so that it will find all the combinations that add up to the whole array, where none of the elements are repeated.Example combinations −[A, B, C, D, E] [A, B, C] [A, B, C, D] [A, B, C, E] [A, B, C] [A, B, C] [D, E]Explanation[A, B, C] [A, B, C] [D, E] and [A, B, C] [D, E] [A, B, C] are the same combinations. Also, the ordering with the subsets doesn't matter as ... Read More

Advertisements