Javascript Articles

Page 291 of 534

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

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 168 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

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

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 257 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

Read More

Group by JavaScript Array Object

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 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

Compute cartesian product of elements in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 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
AmitDiwan
Updated on 21-Nov-2020 272 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

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

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 257 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

Find all occurrences of a word in array in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 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 this will be −const arr = ["word", "a word", "another word"]; const query = "word"; const findAll = (arr, query) => {    let count = 0;    count = arr.filter(el => {       return el.indexOf(query) != -1;    }).length;    return count; }; console.log(findAll(arr, query));OutputAnd the output in the console will be −3

Read More

Group Similar Items in JSON in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 2K+ Views

Suppose, we have a JSON Array that contains data about some tickets like this −const arr = [    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to ...

Read More

Multiply and Sum Two Arrays in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 3K+ Views

We are required to write a JavaScript function that takes in two arrays of equal length. The function should multiply the corresponding (by index) values in each, and sum the results.For example: If the input arrays are −const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1];then the output should be 34, because,(4*2+3*3+4*3+5*1) = 34ExampleThe code for this will be −const arr1 = [2, 3, 4, 5]; const arr2 = [4, 3, 3, 1]; const produceAndAdd = (arr1 = [], arr2 = []) => {    let sum = 0;    for(let i=0; i < arr1.length; i++) {       const product = (arr1[i] * arr2[i]);       sum += product;    };    return sum; }; console.log(produceAndAdd(arr1, arr2));OutputAnd the output in the console will be −34

Read More

Evaluating a string as a mathematical expression in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 3K+ Views

We are required to write a JavaScript function that takes in a stringified mathematical equation. The function should return the result of the equation provided to the function.For example: If the equation is −const str = '1+23+4+5-30';Then the output should be 3ExampleThe code for this will be −const str = '1+23+4+5-30'; const compute = (str = '') => {    let total = 0;    str = str.match(/[+\−]*(\.\d+|\d+(\.\d+)?)/g) || [];    while (str.length) {       total += parseFloat(str.shift());    };    return total; }; console.log(compute(str));OutputAnd the output in the console will be −3

Read More
Showing 2901–2910 of 5,338 articles
« Prev 1 289 290 291 292 293 534 Next »
Advertisements