Tell us what’s happening:
When I tried this
function largestOfFour(arr) {
let x, y = []
x = arr.shift()
console.log(x)// returns 4,5,1,3
y = Math.max(...x)
console.log(y)//returns 5
x = arr.shift()
console.log(x)// return 13,27,18,26
y = Math.max(...x)
console.log(y)//return 27
//etc.
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
It prints the largest number of one array. But when I put this into a loop. It returns “-Infinity”
Your code so far
function largestOfFour(arr) {
let x, y = []
for (let i = 0; i < arr.length; i++){
x = arr[i].shift()
y = Math.max(...x)
}
console.log(y)
return y;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
/** x = arr.shift()
console.log(x)
y = Math.max(...x)
console.log(y) */
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.119
.
Link to the challenge: