amanlad
February 27, 2021, 7:22am
1
Tell us what’s happening:
// i am trying this code from so long and don’t know why it is not working and other people are using apply. and it works I don’t understand it could you please help me
Your code so far
function largestOfFour(arr) {
let tst=[]
for(let i=0;i<arr.length;i++)
{
tst.push(Math.max(arr[i]))
}
return tst;
}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36.
Challenge: Return Largest Numbers in Arrays
Link to the challenge:
Hi @amanlad !
Welcome to the forum!
You are close.
However, you are incorrectly using math.max for an array.
I would suggest looking at the examples from MDN docs. Specifically the example they use for arrays and the spread operator. Then you can make the necessary change.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
2 Likes
Hello @jwilkins.oboe , Welcome to the freecodecamp community.
The array provided in the function call is a 2 dimensional array. Therefore you have to loop twice.
e.g
for(){
//this gets you into the array of arrays so that you can access each embedded array
for(){
//this will let you access each value within a specific array
}
}
1 Like
Hi @cacious8 !
If you run the code with the spread operator you will find that you don’t have to add the extra loop.
Because if you think about it arr[0] would return [4, 5, 1, 3]
and if you use Math.max(...arr[0]) it would return 5.
You could pass the test with just one for loop and the spread operator here
If you console.log(tst) then you will get [ 5, 27, 39, 1001 ]
2 Likes
Yes, you are very much right. I just noticed the person asking the question is a beginner, so I just wanted to spread some clarity.
The spread operator is a better solution though.
2 Likes
amanlad
February 27, 2021, 8:38am
6
thanks a lot for helping me out
I was particularly interested in the Math.max thanks again
Continuing the discussion from Could you please help me :
amanlad
February 27, 2021, 8:40am
7
thanks for responding to my question
system
Closed
August 29, 2021, 7:19pm
9
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.