For loop - codepen

Hello everyone)
I’m doing Wiki Viewer and I have some trouble. When I’m trying to write for loop I have an unknown issue…

Found the solution using reduce. IDK what was the problem with for…

I’m no sure either. You should make your JS on a text editor like repl.it then copy and paste. Its easier.

Hello Denis,

I had similar problem with a loop on CodePen. Consider given code:

function countWindows(window) {
  var i = 1;
  while ( i <= window ){
    console.log(i);
    i++
  }
}


countWindows(10);

It works fine ‘outside’ CodePen but when run in the CodePen it produces an error.

Chrome console output:
‘Uncaught TypeError: Cannot read property ‘shouldStopExecution’ of undefined’

In order to solve it I had to change a name of the parameter from ‘window’ to ‘windows’.

Some words already exist in the current context and are passed to your loop. I believe that your code might have worked if you used different names: urls -> myURL, titles -> someTitle etc.

You can always check if an object exist in the global scope with:
console.log(typeof objectName);
example: console.log(typeof Window);

Good luck,
Cravsky

Yeah, there is a global object called “window”. Like Cravsky suggests, if you put console.log('window', window) at the top of the file, you can see it. I don’t understand why the function doesn’t create it’s own local variable by the same name. This is a pretty standard thing in JS and should be handled. Codepen has some extra protections built into it, perhaps they prevent you from creating a new variable with that name, even in a different scope. Weird.

surround your code with 3 back ticks

1 Like