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 Working with 'for' Loops Create a for Loop

Rotem Aslan
seal-mask
.a{fill-rule:evenodd;}techdegree
Rotem Aslan
Full Stack JavaScript Techdegree Student 9,895 Points

Create a for loop that logs the numbers 5 to 100 to the console. Use the console.log() method to log a value to the cons

why this code not working ?

script.js
var res;
for(let i=0;i<=95;i++){
  res += 'i='+i 
}
console.log(res)

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Rohald van Merode
Treehouse Staff

Hi Rotem Aslan 👋

What you're doing with this loop is create one large string that would end up looking something like i=0i=1i=2i=3i=4i=5.....

For this challenge you'll want to log each number individually to the console. I'd suggest to move the console.log inside your loop and print the i on every iteration. Doing it this way you won't be needing a separate res variable either.

Also please notice that the requirement for the challenge is to print the numbers 5 to 100 while currently your loop starts at 0 up to 95.

Hope this helps 🙂