0

I'm practicing using the map method and i'm stuck on this problem. I'm trying to return the largest number out of each of these arrays but for my function to be complete i need to return the push method outside of my nested map. I have no idea how to do that. Any input would be extremely appreciated. Thanks in advance.

const bigAssArr = [[4, 5, 1, 3], [13, 27, 18 , 26], [32, 35, 37, 39], [100, 1001, 857, 1]]
const biggestFour = [];

const largestOfFour = (arr) => {
  return arr.map((item, index) => {
    let tempMax = item[0];
      return item.map((i, iTwo) => {
        let currentValue = i;
        if (currentValue > tempMax ) {
          tempMax = currentValue
          
        }
      })
      biggestFour.push(tempMax);  // *** This is where i'm having the problem***
  })
}

4
  • 1
    arr.map(x => Math.max(...x)) Commented Jan 17, 2021 at 1:32
  • 1
    It does not make sense to use two maps. Should be on map and one forEach or reduce. You also should not be pushing to the array, you should be returning the value. Commented Jan 17, 2021 at 1:33
  • 1
    A little tip: f you're ever intending to return an array that's a different length than what you started with, .map is the wrong solution. .map returns an array of the same length with the same or different values after transforming them; .filter returns an array of the same or less length based on a predicate, and .reduce takes an array and an initial value and returns whatever you want (reduce is very flexible). That's why your outer map is reasonable, but the inner map doesn't make any sense. Commented Jan 17, 2021 at 1:49
  • @epascarello Hi bro, the Math.max(...arr) has an issue. So pls take a look at the second solution of mine. Thanks ^^! Commented Jan 17, 2021 at 2:33

4 Answers 4

2

You can find the largest in each array as follow.

const bigAssArr = [[4, 5, 1, 3], [13, 27, 18 , 26], [32, 35, 37, 39], [100, 1001, 857, 1]]
var biggestFour = bigAssArr.map( arr => {

  return Math.max(...arr);
  
})

console.log(biggestFour)

Sign up to request clarification or add additional context in comments.

1 Comment

Hi bro, the Math.max(...arr) has an issue. So pls take a look at the second solution of mine. Thanks ^^!
2

In ES6, you can use Math.max(...arr) to get the max number of an array.

The Math.max() function returns the largest of the zero or more numbers given as input parameters.

For example:

 var arr = [4, 5, 1, 3];
 console.log(Math.max(...arr )); // Output: 5

After that, you can use .map to create a new array.

const bigAssArr = [[4, 5, 1, 3], [13, 27, 18 , 26], [32, 35, 37, 39], [100, 1001, 857, 1]]

const largestOfFour = (arr) => {
 return arr.map(items => Math.max(...items))
}

console.log(largestOfFour(bigAssArr));

⚠️ According to @GBra's recommendation, Math.max(...arr) is not an optimal solution. It's slow, if the array contains too many elements it will fail or return the wrong result or hit max call stack size.

[Highly recommend !!!] Another option is to use .reduce to get the max item Of an array

const bigAssArr = [[4, 5, 1, 3], [13, 27, 18 , 26], [32, 35, 37, 39], [100, 1001, 857, 1]]
const getMaxItemOfArray = (arr) => arr.reduce((result, currentItem) => 
{
  return Math.max(currentItem, result);
}, arr[0]);

const result = bigAssArr.map(r => getMaxItemOfArray(r));
console.log(result);

5 Comments

@Ishmael Does this answer your question? Let me know if you need any help.
just keep in mind that Math.max(...arr) is not an optimal solution. It's slow, if the array contains too many elements it will fail or return the wrong result or hit max call stack size. Your second option with reduce is better.
@GBra Yeah, I totally agree with you. Let me take note of the answer as well.
thanks a lot guys i appreciate the guidance
@Ishmael 1012 Does this answer your question? Let me know if you need any help.
0

const myArr = [[4, 5, 1, 3], [13, 27, 18 , 26], [32, 35, 37, 39], [100, 1001, 857, 1]]
let highest=arr=>arr.map(a=>Math.max(...a))
const highestValues=highest(myArr)
console.log(highestValues)

1 Comment

Math.max(...arr) is not an optimal solution. It's slow, if the array contains too many elements it will fail or return the wrong result or hit max call stack size. I would other use .reduce for this.
0

Use map when you want to return a value for each element of an array

Use reduce when you want to return a single value based of the array, reduce will run the callback function for each element of the array, just like map

reduce takes three arguments:

  1. accumulator(this defaults to the first element in the array if no third argument is supplied, in the end reduce will return this value)
  2. current value(this defaults to the second element in the array if no third argument is supplied, otherwise this starts from the first element)
  3. starting accumulator value

If there is only one element in the array, reduce will have no effect.

const bigAssArr = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [100, 1001, 857, 1]]
const largestOfFour = (arr) => {
  return arr.map((item) => {
    return item.reduce((acc, curr) => {
      if (acc > curr) {
        return acc;
      }
      return curr;
    })
  })
}
console.log(...largestOfFour(bigAssArr))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.