Found 6710 Articles for Javascript

JavaScript Array showing the index of the lowest number

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

166 Views

We are required to write a JavaScript function that takes in an array of numbers. Then the function should return the index of the smallest number in the array.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const lowestIndex = arr => {    const creds = arr.reduce((acc, val, ind) => {       let { num, index } = acc;       if(val < num){          num = val;          index = ind;       };       return { num, index };    }, {       num: Infinity,       index: -1    });    return creds.index; }; console.log(lowestIndex(arr));OutputThe output in the console −6

Grouping array values in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:47:14

468 Views

Suppose we have an array of objects containing some years and weeks data like this −const arr = [    {year: 2017, week: 45},    {year: 2017, week: 46},    {year: 2017, week: 47},    {year: 2017, week: 48},    {year: 2017, week: 50},    {year: 2017, week: 52},    {year: 2018, week: 1},    {year: 2018, week: 2},    {year: 2018, week: 5} ];We are required to write a JavaScript function that takes in one such array. The function should return a new array where all the objects that have common value for the "year" property are grouped into ... Read More

How to sort array according to age in JavaScript?

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

416 Views

We are required to write a JavaScript function that takes in an array of numbers representing ages of some people.Then the function should bring all the ages less than 18 to the front of the array without using any extra memory.ExampleThe code for this will be −const ages = [23, 56, 56, 3, 67, 8, 4, 34, 23, 12, 67, 16, 47]; const sorter = (a, b) => {    if (a < 18) {       return -1;    };    if (b < 18) {       return 1;    };    return 0; } const sortByAdults = arr => {    arr.sort(sorter); }; sortByAdults(ages); console.log(ages);OutputThe output in the console −[    16, 12, 4, 8, 3, 23, 56,    56, 67, 34, 23, 67, 47 ]

Grouping and sorting 2-D array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:41:37

858 Views

Suppose we have a two-dimensional array of numbers like this −const arr = [    [1, 3, 2],    [5, 2, 1, 4],    [2, 1] ];We are required to write a JavaScript function that groups all the identical numbers into their own separate subarray, and then the function should sort the group array to place the subarrays into increasing order.Therefore, finally the new array should look like −const output = [    [1, 1, 1],    [2, 2, 2],    [4], [3],    [5] ];ExampleThe code for this will be −const arr = [    [1, 3, 2],   ... Read More

String replace multiple characters with an asterisk in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:39:30

1K+ Views

We are required to write a JavaScript function that takes in a string as the first argument and an array of numbers. Our function should replace all the characters in the string at indices that are specified by the array elements taken as the second argument with an asterisk.ExampleThe code for this will be −const str = "Lorem ipsum dolor sit amet consectetur adipiscing elit"; const arr = [4, 7, 9, 12, 15]; const replceWithAsterisk = (str, indices) => {    let res = '';    res = indices.reduce((acc, val) => {       acc[val] = '*';     ... Read More

Iterate over an object and remove false property in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:37:36

1K+ Views

Suppose, we have a JSON object like this −const obj = {    a: {       someKey: {          propOne: '',          enabled: true       }    },    b: {       someKey: {          propOne: '',          enabled: false       }    },    c: {       someKey: {          propOne: '',          enabled: false       }    },    someKey: {       ab: {     ... Read More

Center of each side of a polygon in JavaScript

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

192 Views

Suppose, we array of arrays like this −const arr = [    [-73.9280684530257, 40.8099975343718],    [-73.9282820374729, 40.8100875554645],    [-73.9280124002104, 40.8103130893677],    [-73.927875543761, 40.8102554080229],    [-73.9280684530257, 40.8099975343718] ];Here each subarray represents a point on a 2-D plane, and each point is a vertex of n sided polygon where n is the number of subarrays in the input arrays.We are required to write a JavaScript function that takes in one such array and returns a new array of n subarrays, each representing the midpoint of corresponding sides of the polygon.ExampleThe code for this will be −const arr = [    [-73.9280684530257, 40.8099975343718],    [-73.9282820374729, 40.8100875554645],    [-73.9280124002104, 40.8103130893677],    [-73.927875543761, 40.8102554080229],    [-73.9280684530257, 40.8099975343718] ]; const findCenters = arr => {    const centerArray = [];    for(i = 0; i

Combine two different arrays in JavaScript

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

397 Views

Suppose we have two arrays, the first array contains the scheduled date for some events and the second array contains the names of those events, like this −const dates = [    {       id:"1",       date:"2017-11-07"    },    {       id:"1",       date:"2017-11-08"    },    {       id:"2",       date:"2017-11-07"    },    {       id:"2",       date:"2017-11-08"    } ]; const names = [    {       id:"1",       name:"Pervies, Peter"    },    {   ... Read More

Finding number of occurrences of the element appearing most number of times in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:26:58

436 Views

We are required to write a JavaScript function that takes in an array of literals and returns the count of elements that appears for the most number of times in the array.ExampleThe code for this will be −let arr = [2, 8, 4, 8, 6, 4, 7, 8]; const countOccurence = arr => {    const max = arr.reduce((acc, val) => {       return Math.max(acc, val);    }, -Infinity);    const count = arr.filter(el => {       return el === max;    });    const { length } = count;    return length; }; console.log(countOccurence(arr));OutputThe output in the console −3

How to filter out common array in array of arrays in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:25:35

250 Views

Suppose we have an array of arrays like this −const arr = [    [       "Serta",       "Black Friday"    ],    [       "Serta",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ],    [       "Simmons",       "Black Friday"    ] ];We are required to write a JavaScript function that ... Read More

Advertisements