Found 6710 Articles for Javascript

Picking the largest elements from multidimensional array in JavaScript

Nikhilesh Aleti
Updated on 19-Dec-2022 17:00:28

3K+ Views

Given a multidimensional array and the task is to pick the largest elements from it in JavaScript. The multidimensional arrays are an arrays inside an array. whenever there are arrays inside the array it will work as multidimensional array. Following is the one-dimensional array - const arr = ["Welcome", "to", "tutorials", "point"]; const arr = [1, 5, 12, 67, 99]; This is how the multidimensional array look like - const array=[["Mike tyson", "Vijay"], ["ananya", "charmee"], ["Lion", "Tiger"]]; This is how we can access the elements from multidimensional array - const array = [["Mike tyson", "Vijay"], ["ananya", ... Read More

Cumulative average of pair of elements in JavaScript

AmitDiwan
Updated on 20-Oct-2020 12:00:52

228 Views

We have an array of numbers and 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.ExampleThe code for this will be −const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; const consecutiveAverage = arr => {    return arr.map((el, ind, array) => {       const first = (array[ind-1] || ... Read More

Inserting empty string in place of repeating values in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:58:16

449 Views

We have to write a function that takes in an array, removes all duplicates from it and inserts the same number of empty strings at the end.For example: If we find 4 duplicate values we have to remove then all and insert four empty strings at the end.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [1, 2, 3, 1, 2, 3, 2, 2, 3, 4, 5, 5, 12, 1, 23, 4, 1]; const deleteAndInsert = arr => {    const creds = arr.reduce((acc, val, ind, array) => {       ... Read More

Detecting the largest element in an array of Numbers (nested) in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:55:28

204 Views

We have to write a simple function in JavaScript that takes in an array of Numbers (nested to any level) and return the greatest number present in the array.For example: If the input array is −const arr = [    34, 65, 67,    [       43, 76, 87, 23, 56, 7,       [          54, 7, 87, 23, 79, 994, 2       ],       54    ], 54, 4, 2 ];Then the output should be −994We will use recursion to find the greatest number in the array.ExampleThe code ... Read More

Detecting the first non-unique element in array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:53:28

169 Views

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).So, 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.ExampleThe code for this will be −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => {    for(let i ... Read More

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

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

507 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];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => { ... Read More

Combining the identical entries together in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:49:38

97 Views

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.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:48:03

628 Views

We are required to write a JavaScript array function that takes in a nested array with false values as well 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];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], ... Read More

If the element repeats, remove all its instances from array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:46:45

102 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];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 of searched string if it exists, otherwise ... Read More

Finding product of Number digits in JavaScript

Nikhilesh Aleti
Updated on 23-Sep-2022 11:13:42

1K+ Views

We are required to write a JavaScript program that takes in a number and finds the product of all of its digits. Input output scenarios Assume there is a number digit assigned to a variable and task is to find the product of the number. Input = 12345; Output = 120 Let’s look into another scenario, where we convert the string number digit into int value and multiply them. Input = "12345"; Output = 120 Math.floor() Math.floor() function in JavaScript returns the largest integer which is less than equal to a given number. In simple ... Read More

Advertisements