Global scope doesn't work in functions?

  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?

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…

2 Likes

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

Happy hacking :slight_smile: