Found 6686 Articles for Javascript

JavaScript Array: Checking for multiple values

AmitDiwan
Updated on 14-Oct-2020 07:22:36

161 Views

We are required to write a JavaScript function that takes in two arrays of Numbers and checks whether all the elements of the first array exist in the second or not.ExampleThe code for this will be −const arr1 = [34, 78, 89]; const arr2 = [78, 67, 34, 99, 56, 89]; const multipleIncludes = (first, second) => {    const indexArray = first.map(el => {       return second.indexOf(el);    });    return indexArray.indexOf(-1) === -1; } console.log(multipleIncludes(arr1, arr2));OutputThe output in the console −true

Padding a string with random lowercase alphabets to fill length in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:20:47

206 Views

We are required to write a function that takes in two arguments, first is a string and second is a number. The length of string is always less than or equal to the number. We have to insert some random lowercase alphabets at the end of the string so that its length becomes exactly equal to the number and we have to return the new string.ExampleLet’s write the code for this function −const padString = (str, len) => {    if(str.length < len){       const random = Math.floor(Math.random() * 26);       const randomAlpha = String.fromCharCode(97 + ... Read More

Pushing NaN to last of array using sort() in JavaScript

AmitDiwan
Updated on 14-Oct-2020 07:18:43

293 Views

We have an array that contains String and number mixed data types, we have to write a sorting function that sorts the array so that the NaN values always end up at the bottom.The array should contain all the valid numbers in the front, followed by string literals, followed by NaN.The code for this will be −const arr = [344, 'gfd', NaN, '', 15, 'f', 176, NaN, 736, NaN, 872, 859, 'string', 13, 'new', NaN, 75]; const sorter = (a, b) => {    if(a !== a){       return 1;    }else if(b !== b){       ... Read More

Pushing positives and negatives to separate arrays in JavaScript

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

315 Views

We are required to write a function that takes in an array and returns an object with two arrays positive and negative. They both should be containing all positive and negative items respectively from the array.We will be using the Array.prototype.reduce() method to pick desired elements and put them into an object of two arrays.ExampleThe code for this will be −const arr = [97, -108, 13, -12, 133, -887, 32, -15, 33, -77]; const splitArray = (arr) => {    return arr.reduce((acc, val) => {       if(val < 0){          acc['negative'].push(val);       }else{ ... Read More

Find the closest value of an array in JavaScript

AmitDiwan
Updated on 12-Oct-2020 11:50:24

160 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and a number as the second argument. The function should then return the number from the array which closest to the number given to the function as second argument.ExampleThe code for this will be −const arr = [3, 56, 56, 23, 7, 76, -2, 345, 45, 76, 3]; const num = 37 const findClosest = (arr, num) => {    const creds = arr.reduce((acc, val, ind) => {       let { diff, index } = acc;       ... Read More

JavaScript Array showing the index of the lowest number

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

103 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

386 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

312 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

683 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

Advertisements