Found 10483 Articles for Web Development

Build maximum array based on a 2-D array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:56:22

122 Views

Let’s say, we have an array of arrays of Numbers like below −const arr = [   [1, 16, 34, 48],   [6, 66, 2, 98],   [43, 8, 65, 43],   [32, 98, 76, 83],   [65, 89, 32, 4], ];We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray.So, for the above array, the output should be −const output = [    48,    98,    65,    83,    89 ];ExampleFollowing is the code to get the greatest element from each subarray −const arr = [    [1, 16, 34, 48],    [6, 66, 2, 98],    [43, 8, 65, 43],    [32, 98, 76, 83],    [65, 89, 32, 4], ]; const constructBig = arr => {    return arr.map(sub => {       const max = Math.max(...sub);       return max;    }); }; console.log(constructBig(arr));OutputThis will produce the following output in console −[ 48, 98, 65, 98, 89 ]

Array of adjacent element's average - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:55:17

271 Views

Let’s say, we have an array of numbers −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1];We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned.Let’s write the code for this function, we will use the Array.prototype.map() function to solve this problem −Exampleconst arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => {    return ... Read More

Finding the first redundant element in an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:53:51

194 Views

Let’s say, we are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory).Therefore, let's write the solution for this problem. We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy.ExampleFollowing is the code −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => {    for(let ... Read More

Inserting element at falsy index in an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:51:31

193 Views

We are required to write an Array function, let’s say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array.If there are no empty spaces, the element should be inserted at the last of the array.We will first search for the index of empty position and then replace the value there with the value we are provided with.ExampleFollowing is the code −const arr = [13, 34, 65, null, 64, false, 65, 14, undefined, 0, , 5, , 6, ,85, ,334]; const pushAtFalsy = function(element){ ... Read More

Alternative shuffle in JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:50:25

344 Views

Alternative ShuffleAn alternatively shuffled array in JavaScript is an array of Numbers in which numbers are indexed such that greatest number is followed by the smallest element, second greatest element is followed by second smallest element and so on.For example: If the input array is −const arr = [11, 7, 9, 3, 5, 1, 13];Then the output should be &minusconst output = [13, 1, 11, 3, 9, 5, 7];ExampleFollowing is the code −const arr = [11, 7, 9, 3, 5, 1, 13]; const sorter = (a, b) => a - b; const alternateShuffle = (arr) => {    const array ... Read More

Using recursion to remove consecutive duplicate entries from an array - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:49:18

524 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space.For example, if the input array is −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1];Then the output should be −const output = [17, 12, 354, 1];ExampleFollowing is the code −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => {    if(len < arr.length){       if(deletable){          arr.splice(len, 1);          len--;       }       return comp(arr, len+1, arr[len] === arr[len+1])    };    return; }; comp(arr); console.log(arr);OutputThis will produce the following output in console −[ 17, 12, 354, 1 ]

Grouping identical entries into subarrays - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:47:12

162 Views

Let’s say, we have an array of numbers that have got identical entries. We are required to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example: If the input array is −const arr = [234, 65, 65, 2, 2, 234];Then the output should be −const output = [[234, 234], [65, 65], [2, 2]];We will use a hashmap to keep a track of the elements already occurred and iterate over the array using a for loop.ExampleFollowing is the code −const arr = [234, 65, 65, ... Read More

Flattening an array with truthy/ falsy values without using library functions - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:46:05

161 Views

We are required to write a JavaScript array function that takes in a nested array with falsy values and returns an array with all the elements present in the array without any nesting.For example − If the input is −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]];Then the output should be −const output = [1, 2, 3, 4, 5, false, 6, 5, 8, null, 6];ExampleFollowing is the code −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]]; const flatten = function(){    let res = [];   ... Read More

Converting array to object by splitting the properties - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:44:54

216 Views

We have an array of string literals in which each element has a dash (-), The property key is present to the left of dash and its value to the right. A sample input array would look something like this −const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "postion-CAM", "languages-German, English, Spanish", "club-Chelsea"];We are required to write a function that splits these strings and form an object out of this array.Let’s write the code, it will loop over the array splitting each string and feeding it into the new object.ExampleFollowing is the code −const arr = ["playerName-Kai Havertz", "age-21", "nationality-German", "postion-CAM", ... Read More

Removing redundant elements from array altogether - JavaScript

AmitDiwan
Updated on 18-Sep-2020 12:43:39

313 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array.For example, if the input is −const arr = [763, 55, 43, 22, 32, 43, 763, 43];The output should be −const output = [55, 22, 32];We will be using the following two methods −Array.prototype.indexOf() −It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf()It returns the index of last occurrence ... Read More

Advertisements