Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Review For Loops and Loop Exiting

Look at the following code examples. All but one of them is an endless loop. Select the one that is NOT an endless loop.

Look at the following code examples. All but one of them is an endless loop. Select the one that is NOT an endless loop.

A
for (var i = 0; i < 10; i -= 1) { console.log( i ); } B
for (var i = 0; i <100; i += 10) { console.log( i ); } C var counter = 1; while (counter > 0) { console.log(counter); counter += 1; } D
var counter = 1; while (counter < 10) { console.log(counter); } I think there is two answer for this question A & C

2 Answers

LaVone Li
PLUS
LaVone Li
Courses Plus Student 5,895 Points

the correct answer for this is :

for (var i = 0; i <100; i += 10) { console.log( i ); }

Simon Coates
Simon Coates
28,694 Points

fine, goes up by tens.

for (var i = 0; i <100; i += 10) {
  console.log( i );
}

The following doesn't change the loop variable.

var counter = 1;
while (counter < 10) {
  console.log(counter);
}

In the following, counter gets bigger, will always be greater than 0. Infinite.

var counter = 1;
while (counter > 0) {
  console.log(counter);
  counter += 1;
}

In the following, loop variable goes in wrong direction. Infinite.

for (var i = 0; i < 10; i -= 1) {
  console.log( i );
}