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

Martin Coutts
Martin Coutts
18,154 Points

How can I get a range of between 4 and 156 on my counter in a for loop?

Struggling to get my counter to start logging between the values of 4 and 156.

var html = '';

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

Tried putting a range in and I think thats where the problem is but not sure

script.js
var html = '';

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

Hi Martin

Just looked at your code and found some inconsistencies. Compare my part with your code and try it out. The for-loop comes with 3 parts. You had declared the counter variable two times.

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

Keep Coding and have fun greets Thomas

you can put as this: -you don't have to assign your variable to an empty string. -You can control the starting point from the first variable (i) in the for loop.

var html; 
for (var i=4; i<=156; i++){  
  html = '<div>' + [i] + '</div>';
  console.log(html);
}
Martin Coutts
Martin Coutts
18,154 Points

Can understand what you's mean and it seems to be working better however the only issue is its coming up with

"Bummer! The console should write out 4 the first time in the loop and 156 the last time through the loop"

Really not sure how to solve this

2 Answers

Steven Ang
Steven Ang
41,751 Points

The challenge just wants you to log the numbers into the console, not the DOM. Remove the variable, it's not needed.

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

The output in the console:

4
5
6
7
...

This code here:

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

Will give the outputs of this:

<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
...

which is wrong, they're strings now, not numbers.

Martin Coutts
Martin Coutts
18,154 Points

That worked, clearly me trying to overcomplicate things.

Thanks guys