Javascript Articles

Page 372 of 534

Grouping names based on first letter in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 1K+ Views

Suppose, we have an array of names like this −const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"];We are required to write a JavaScript function that takes in one such array. The function should return an array of objects with two properties −letter -> the letter on which the names are groupednames -> an array of names that falls in that groupExampleThe code for this will be −const arr = ["Simon", "Mike", "Jake", "Lara", "Susi", "Blake", "James"]; const groupNames = arr => {    const map = arr.reduce((acc, val) => {       let char = val.charAt(0).toUpperCase();   ...

Read More

How to convert array into array of objects using map() and reduce() in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 993 Views

Suppose we have an array of arrays like this −const arr = [    [       ['juice', 'apple'], ['maker', 'motts'], ['price', 12]    ],    [       ['juice', 'orange'], ['maker', 'sunkist'], ['price', 11]    ] ];We are required to write a JavaScript function that takes in one such array and returns a new array of objects built based on the input array.So, for the above array, the output should look like this −const output = [    {juice: 'apple', maker: 'motts', price: 12},    {juice: 'orange', maker: 'sunkist', price: 11} ];ExampleThe code for this will be ...

Read More

How to convert array of decimal strings to array of integer strings without decimal in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 736 Views

We are required to write a JavaScript function that takes in an array of decimal strings. The function should return an array of strings of integers obtained by flooring the original corresponding decimal values of the array.For example, If the input array is −const input = ["1.00","-2.5","5.33333","8.984563"];Then the output should be −const output = ["1","-2","5","8"];ExampleThe code for this will be −const input = ["1.00","-2.5","5.33333","8.984563"]; const roundIntegers = arr => {    const res = [];    arr.forEach((el, ind) => {       const strNum = String(el);       res[ind] = parseInt(strNum);    });    return res; }; console.log(roundIntegers(input));OutputThe output in the console −[ 1, -2, 5, 8 ]

Read More

Group by element in array JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 469 Views

Suppose, we have an array of objects like this −const arr = [    {"name": "toto", "uuid": 1111},    {"name": "tata", "uuid": 2222},    {"name": "titi", "uuid": 1111} ];We are required to write a JavaScript function that splits the objects into separate array of arrays that have the similar values for the uuid property.OutputTherefore, the output should look like this −const output = [    [       {"name": "toto", "uuid": 1111},       {"name": "titi", "uuid": 1111}    ],    [       {"name": "tata", "uuid": 2222}    ] ];The code for this will be −const ...

Read More

Find array elements that are out of order in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 308 Views

Suppose we have an array of sorted numbers but some elements of the array are out of their sorted order.We are required to write a JavaScript function that takes in one such array and returns a subarray of all those elements that are out of order.ExampleThe code for this will be −const arr = ["2", "3", "7", "4", "5", "6", "1"]; const findOutOfOrder = arr => {    let notInOrder = [];    notInOrder = arr.filter((el, ind) => {       return ind && this.next !== +el || (this.next = +el + 1, false);    }, {       next: null    });    return notInOrder; }; console.log(findOutOfOrder(arr));OutputThe output in the console −[ '7', '1' ]

Read More

Return the index of first character that appears twice in a string in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 331 Views

We are required to write a JavaScript function that takes in a string and returns the index of first character that appears twice in the string.If there is no such character then we should return -1.ExampleThe code for this will be −const str = 'Hello world, how are you'; const firstRepeating = str => {    const map = new Map();    for(let i = 0; i < str.length; i++){       if(map.has(str[i])){          return map.get(str[i]);       };       map.set(str[i], i);    };    return -1; }; console.log(firstRepeating(str));OutputFollowing is the output on console −2

Read More

Sum of distinct elements of an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 258 Views

Suppose, we have an array of numbers like this −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1];We are required to write a JavaScript function that takes in one such array and counts the sum of all distinct elements of the array.For example:The output for the array mentioned above will be −30ExampleThe code for this will be −const arr = [1, 5, 2, 1, 2, 3, 4, 5, 7, 8, 7, 1]; const distinctSum = arr => {    let res = 0;    for(let i = 0; i < arr.length; i++){       if(i === arr.lastIndexOf(arr[i])){          res += arr[i];       };       continue;    };    return res; }; console.log(distinctSum(arr));OutputFollowing is the output on console −30

Read More

Finding the first unique element in a sorted array in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 595 Views

Suppose we have a sorted array of literals like this −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9];We are required to write a JavaScript function that takes in one such array and returns the first number that appears only once in the array.If there is no such number in the array, we should return false.For this array, the output should be 6.ExampleThe code for this will be −const arr = [2, 2, 3, 3, 3, 5, 5, 6, 7, 8, 9]; const firstNonDuplicate = arr => {    let appeared = false;   ...

Read More

Reverse alphabetically sorted strings in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 354 Views

We are required to write a JavaScript function that takes in a lowercase string and sorts it in the reverse order i.e., b should come before a, c before b and so on.For example:If the input string is −const str = "hello";Then the output should be −const output = "ollhe";The code for this will be −const string = 'hello'; const sorter = (a, b) => {    const legend = [-1, 0, 1];    return legend[+(a < b)]; } const reverseSort = str => {    const strArr = str.split("");    return strArr    .sort(sorter)    .join(""); }; console.log(reverseSort(string));Following is the output on console −ollhe

Read More

Converting array of Numbers to cumulative sum array in JavaScript

AmitDiwan
AmitDiwan
Updated on 10-Oct-2020 627 Views

We have an array of numbers like this −const arr = [1, 1, 5, 2, -4, 6, 10];We are required to write a function that returns a new array, of the same size but with each element being the sum of all elements until that point.Therefore, the output should look like −const output = [1, 2, 7, 9, 5, 11, 21];Therefore, let’s write the function partialSum(), The full code for this function will be −const arr = [1, 1, 5, 2, -4, 6, 10]; const partialSum = (arr) => {    const output = [];    arr.forEach((num, index) => { ...

Read More
Showing 3711–3720 of 5,338 articles
« Prev 1 370 371 372 373 374 534 Next »
Advertisements