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 Create a for Loop

gregory gordon
seal-mask
.a{fill-rule:evenodd;}techdegree
gregory gordon
Full Stack JavaScript Techdegree Student 14,652 Points

Create a for loop that logs the numbers 4 to 156 to the console. To log a value to the console use the console.log( ) me

can not complete challenge.

script.js
var html = ' ';
for ( var i = 4; i <= 159; i += 1 ) {
  html += ' ' + i + ' '; console.log(html);
}

2 Answers

Hey Gregory,

It looks like you have a lot going on in your code that is unnecessary. You don't need to initialize any other variable besides the one you use directly inside the for loop. Also, since the challenge wants from 4 to 156, you need the condition to check when i is one below the max number you want. In this case, we want i < 157 because when it gets above 156, the for loop will stop executing. And then you only need to use console.log with i. There's no need for any other variables.

for (var i = 4; i < 157; i += 1) {
console.log(i);
}
gregory gordon
seal-mask
.a{fill-rule:evenodd;}techdegree
gregory gordon
Full Stack JavaScript Techdegree Student 14,652 Points

Once i posted this i realized what i did wrong i changed my code and it worked but i will still give you best answer credit since it is the correct answer. Thanks for helping out the community.

My pleasure, Gregory! Happy Coding!

Brandon Miller
Brandon Miller
6,818 Points

the <= 157 should be < 157, eh?.... not sure why I thought it needed an equals sign to be true.

Moises Miguel
Moises Miguel
5,028 Points

HI gregory, can you tell me how you solved this please?