Found 6686 Articles for Javascript

Retaining array elements greater than cumulative sum using reduce() in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:13:57

87 Views

We are required to write a JavaScript function that takes in an array of Numbers. Our function should return a new array that contains all the elements from the original array that are greater than the cumulative sum of all elements up to that point. We are required to solve this problem using the Array.prototype.reduce() function.ExampleLet’s write the code for this function −const arr = [1, 2, 30, 4, 5, 6]; const retainGreaterElements = arr => {    let res = [];    arr.reduce((acc, val) => {       return (val > acc && res.push(val), acc + val);    }, 0);    return res; } console.log(retainGreaterElements(arr));OutputThe output in the console −[1, 2, 30]

Cumulative sum of elements in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:12:19

1K+ Views

Suppose, we have an array of numbers like this −const arr = [1, 2, 3, 4, 5, 6];We are required to write a JavaScript function that takes in one such array and returns a new array with corresponding elements of the array being the sum of all the elements upto that point from the original array.Therefore, for the above array, the output should be −const output = [1, 3, 6, 10, 15, 21];ExampleThe code for this will be −const arr = [1, 2, 3, 4, 5, 6]; const findCumulativeSum = arr => {    const creds = arr.reduce((acc, val) => ... Read More

Replace all occurrence of specific words in a sentence based on an array of words in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:10:32

419 Views

We are required to write a JavaScript function that takes a string and an array of strings.Our function should return a new string, where all the occurrences of the word in the string that are present in the array are replaced by a whitespace.Our function should use the String.prototype.replace() method to solve this problem.ExampleThe code for this will be −var excludeWords = ["A", "ABOUT", "ABOVE", "ACROSS", "ALL", "ALONG", "AM", "AN", "AND", "ANY", "ASK", "AT", "AWAY", "CAN", "DID", "DIDN'T", "DO", "DON'T", "FOR", "FROM", "HAD", "HAS", "HER", "HIS", "IN", "INTO", "IS", "IT", "NONE", "NOT", "OF", "ON", "One", "OUT", "SO", "SOME", "THAT", "THE", ... Read More

Group strings starting with similar number in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:09:03

242 Views

Suppose, we have an array of Numbers represented by strings like this −const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"];We are required to write a JavaScript function that takes in one such array and groups all the strings starting with same number in a common subarray.Therefore, the output of our function should look like −const output = [["1.1", "1.2", "1.3"], ["2.1", "2.2"], ["3.1", "3.2", "3.3"], ["4.1", "4.2"]];ExampleThe code for this will be −const arr = ["1.1", "1.2", "1.3", "2.1", "2.2", "3.1", "3.2", "3.3", "4.1", "4.2"]; const groupSimilarStarters = arr => {    let res = ... Read More

Calculating quarterly and yearly average through JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:06:19

193 Views

Suppose, we have an array of Numbers like this −const arr = [1, 2, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];We are required to write a JavaScript function that takes in one such array and chunks the array into quarterly and yearly groups intermediately.The groups for the above array should look something like this −const quarterly = [[1, 2, 2], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20]]; const yearly = [[1, 2, 2, 4, 5, 6, 7, 8, 9, ... Read More

Map numbers to characters in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:04:32

913 Views

Suppose we have the number 12145. We are required to write a function that maps the digits of the number to English alphabets according to the following norms. The alphabets are to be mapped according to the 1 based index, like 'a' for 1 and 'b' for 2 'c' for 3 and so on.There can be several ways for mapping a number. Let's take the above number 121415 for example, It can be mapped as −12145->1, 2, 1, 4, 5->a, b, a, d, eIt also can be −12145->12, 1, 4, 5->l, a, d, eIt can also be −12145->12, 14, 5->l, ... Read More

JavaScript Separate objects based on properties

AmitDiwan
Updated on 12-Oct-2020 11:02:09

1K+ Views

Suppose, we have an object like this −const obj = {    0: { "time": 1, "day": 1, },    1: { "time": 2, "day": 1, },    2: { "time": 3, "day": 1, },    3: { "time": 1, "day": 2, },    4: { "time": 2, "day": 2, },    5: { "time": 3, "day": 2, } };We are required to write a JavaScript function that takes one such object and groups all the key value pairs in separate sub objects that have the same value for the day key.OutputThe output for the above object should be −const ... Read More

Finding shortest word in a string in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:23:40

611 Views

We are required to write a JavaScript function that takes in a string and returns the shortest word from the string.For example: If the input string is −const str = 'This is a sample string';Then the output should be −const output = 'a';ExampleThe code for this will be −const str = 'This is a sample string'; const findSmallest = str => {    const strArr = str.split(' ');    const creds = strArr.reduce((acc, val) => {       let { length, word } = acc;       if(val.length < length){          length = val.length;   ... Read More

Dynamic Programming: return all matched data in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:21:59

242 Views

Suppose we have a JSON object that have information about the location of some cities of some countries like this −const countryInfo = {    country: [{       name: "Bangladesh",       province: [{          name:"Dhaka",          city: [{             name:"Tangail",             lat: '11'          }, {             name:"Jamalpur",             lat: '12'          }]       }, {          name: "Khulna", ... Read More

Get values that are not present in another array in JavaScript

AmitDiwan
Updated on 10-Oct-2020 08:16:21

1K+ Views

We are given two arrays: (arr1 and arr2) −arr1 contains some literal values.arr2 contains objects that map some literal values.We are required to write a JavaScript function that takes in two such arrays. Then the function should return an array of all the elements from arr1 that are not mapped by objects in arr2.ExampleThe code for this will be −const arr1 = [111, 222, 333, 444]; const arr2 = [    { identifier: 111 },    { identifier: 222 },    { identifier: 444 }, ]; const getAbsentValues = (arr1, arr2) => {    let res = [];    res = arr1.filter(el => {       return !arr2.find(obj => {          return el === obj.identifier;       });    });    return res; }; console.log(getAbsentValues(arr1, arr2));OutputThe output in the console −[ 333 ]

Advertisements