Algorithm scripting challenge alternate solution?

Tell us what’s happening:
Hello hello, as I was working through this challenge, I researched a bit about passing arrays into the Math.max function and found that you can do it with a spread operator. As far as i can tell, the solution I came up with will return the correct answer, but I’m guessing freecodecamp wants me to use one of the solutions they provide. I’m wondering if it’s because I declared an empty array outside of the function. Can anyone tell me if my solution is actually acceptable for a challenge like this? I want to understand if there is a big difference with my solution and the ones provided. Please help!

Your code so far


let newArr = [];
function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++) {
  newArr.push(Math.max(...arr[i])) 
}
return newArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

You do want to pull that array inside of the function. Global variables can lead to unintended consequences. Once you fix that, your solution is fine.

It is worth figuring out how to do this with nested loops though.

ahhhhh right right, thank you! I will do it again with a nested loop as well :slight_smile:

1 Like