Code runs well in other IDE but not in Codepen

Tell us what’s happening:
My code works in other IDE, and I see the correct result returned by that, but in Codepen I see “ReferenceError: maxArr is not defined”.

Your code so far


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

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

Your browser information:

User Agent is: Chrome Version 78.0.3904.108 (Official Build) (64-bit).

Challenge: Return Largest Numbers in Arrays

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

Welcome, gkurmanov.

You have not declared what maxArr is. Javascript is a very simple language, relative to most, but you still need to declare your variables with let or const.

It makes no sense that this code would work in another environment.

Hope this helps.

1 Like

Your code should work on Codepen, the variable will just automatically become a global (not a good idea).

console.log(window.maxArr)

It will however not work for the curriculum challenge because the code is running in strict mode.

Ufff. Thank you, this is my habitual mistake. Nevertheless, the main point remains actual. Why does the solution run when outputting to other editor(like here habitual mistake. Nevertheless, the main question remains. Why does the solution run when outputting to, for instance, https://repl.it/@kotrotko/JS-Codepen-ES5-task-4, but not to CodePen? JS can be a simple language and can be a complex language, but shouldn’t the code behave the same way in different editors?

Your solution does not error out in Codpen. It does not display to the console because you did not tell it to with a console.log statement.

The code works across browsers but may display differently across IDEs because there is no standard for an IDE. Repl.it projects will always display the last evaluation to the console, but it is not the browser console. It is a console they have created to act as normal console. In fact, it is more like Node which does the same thing. However, the browser console will only display content if you use a console.log.

1 Like

Thank you, it is exactly what I would like to know (about difference between IDEs).