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

'for' loop between two numbers

It looks like I went to far and I don't know how to reel it in.

My Code:

var between = ''; for ( var i = 4; i <= 156; i += 1) { between += i; console.log( between ); }

The Challenge throws back this "The console should write out 4 the first time in the loop and 156 the last time through the loop."

And it looks like in the console it does 4 to 156 but in the last one after maybe 152 loops, I'm guessing.

Soooo... I may not be understanding the question.

1 Answer

Steven Parker
Steven Parker
229,732 Points

This program generates a lot of digits, but they aren't numbers, they are strings. And because of concatenation each string printed out gets larger and larger.

But you're right, you can fix this just by simplifying. Your loop already runs from 4 to 156 with the loop variable "i". So what if you were to use the variable i as the argument to your console.log call? Then you could eliminate the "between" variable altogether.