Global scope doesn't work in functions?

Global scope doesn't work in functions?
0

#1
  let res = [];

function largestOfFour(arr) {
  for(let i of arr){
    res.push((Math.max(...i)))
  }
  return res;
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]])

this code doesn’t seem to be working, but as soon as I put let res = []; inside of the function it works… I thought that since I declared res in global scope it should be accessible in all of the functions I declare…? so why didn’t it work when it was outside?


#2

I think I got it… so since it’s outside my function it won’t reset the “res” array every time I run the function, meaning that it will keep pushing the numbers to the end of it…


#3

Please mark your answer as solution if it’s resolved.

Happy hacking :slight_smile: