AmitDiwan has Published 10744 Articles

Find specific key value in array of objects using JavaScript

AmitDiwan

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"    }], ... Read More

Generating Random Prime Number in JavaScript

AmitDiwan

AmitDiwan

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

602 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 = ... Read More

Iterating through an array, adding occurrences of a true in JavaScript

AmitDiwan

AmitDiwan

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

139 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. ... Read More

How to count the occurrence of a specific string in a string in JavaScript

AmitDiwan

AmitDiwan

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

206 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 ... Read More

Group by JavaScript Array Object

AmitDiwan

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 ... Read More

Generating random string with a specific length in JavaScript

AmitDiwan

AmitDiwan

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

956 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 ... Read More

Compute cartesian product of elements in an array in JavaScript

AmitDiwan

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 ... Read More

Sorting array of strings having year and month in JavaScript

AmitDiwan

AmitDiwan

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

241 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 ... Read More

How to find all partitions of a multiset, where each part has distinct elements in JavaScript

AmitDiwan

AmitDiwan

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

214 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, ... Read More

Find all occurrences of a word in array in JavaScript

AmitDiwan

AmitDiwan

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

1K+ Views

We are required to write a JavaScript function that takes in an array of literals as the first argument and a string as the second argument. Our function should return the count of the number of times that string (provided by second argument) appears anywhere in the array.ExampleThe code for ... Read More

Advertisements