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

Conner Williams
PLUS
Conner Williams
Courses Plus Student 3,305 Points

is there a way to think about this concept that makes it easier to understand?

I am having trouble wrapping my head around the correct coding for these concepts.

script.js
var html = '';

for ( var i = 4;  i<=157; i += 1 ) {
     html += '<div>' + i + '</div>';
     }
     console.log(html);

1 Answer

Matthew Long
Matthew Long
28,407 Points

Looks like you're over complicating the challenge. It never asked you to log an HTML <div> tag with a number inside it to the console. It only wanted the number logged to the console. Every number. To log every number each time the loop is ran you'll want console.log() inside the loop body. You set up the loop itself correctly (except for including 157), which would typically be the most difficult part of this challenge. So good job on that!

for (var i = 4; i <= 156; i += 1) {
  console.log(i);
}
Conner Williams
Conner Williams
Courses Plus Student 3,305 Points

Thanks! this answer plus the loop practice in the library cleared it right up.