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

I can't understand why this piece of Javascript code isn't working

I have created this piece of code for a project:

for (let current = 1; current % 7 == 0; current = current + 1) {
 console.log(current);
}

I want console.log to display all numbers starting from 0, until it reaches a number that its remainder when divided by seven is zero (obviously that number will be seven)...

Why console.log is not showing the numbers 1, 2, 3, 4, etc...? What I'm doing wrong?

Thanks

2 Answers

In the first example only 1 to 6 are logged. When current = 7 the condition 0 > 0 is false and the loop stops.

Thank you very much Kris...now I understand

I got the following to work

for (let current = 1; current % 7 > 0; current = current + 1) {
 console.log(current);
}

I think your condition to run is false so it never runs. It would be like

for (let current = 1; current ==100; current = current + 1) {
 console.log(current);
}

which also doesn't run but this does:

for (let current = 1; current ==1; current = current + 1) {
 console.log(current);
}

Thank you for your answer, but what I still can't understand from your first example is why the remainder of 7 divided by seven, which is zero, should be greater than zero? Zero can't be greater than zero, so why Javascript evaluates this condition as true?