Iterate with JavaScript For Loops/"Run The Tests" button doesn't work

Tell us what’s happening:
Hi!
I’m trying to run the test for my code, but the page keeps freezing every time I hit “Run the tests” button.
I’ve already refreshed page many times, cleared my cache, and tried to go to the next task, but the next task has the same issue.
I tried to check previous tasks, they worked. Issues start only with “loop” tasks.
Would anyone please help me with that?

Your code so far


// Example
var ourArray = [];

for (var i = 0; i < 5; i++) {
  ourArray.push(i);
}

// Setup
var myArray = [];
for (var a = 0; a > 0 || a < 6; a++){
  myArray.push(a);
}
// Only change code below this line.


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops

The for loop condition you have chosen will always be true:

a > 0 || a < 6

0 is less than 6 (true)
1 is greater than 0 OR 1 is less than 6 (true)
2 is greater than 0 OR 2 is less than 6 (true)
3 is greater than 0 OR 3 is less than 6 (true)
4 is greater than 0 OR 4 is less than 6 (true)
5 is greater than 0 OR 5 is less than 6 (true)
6 is greater than 0 (true)
7 is greater than 0 (true)
.
.
.
and so on forever

Remember when you use || (the OR operator) between two expressions, only one of the expression has to be true for the entire statement to be true.

1 Like

Randell, thank you for your fast reply!
It works now!

To add to @RandellDawson’s answer (because this would’ve been helpful way down the road had I learned it earlier) the || not only asks if any of the statements are true, but it also defaults to the first expression that results as true. This is useful when choosing return statements or setting variables:

const apiCall = someApi('all info') //pretend this either returns a large array, or NULL if nothing is available

return apiCall || true || 'no data' //if there's no data in apiCall it will default to the second "true," but it will never run a check on 'no data' since it already found a truthy value
1 Like

thank you for sharing your knowledge!

1 Like