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

Blake Atkerson
Blake Atkerson
2,797 Points

I am unsure why this differs from the instructor's example

I copied the same logic from the video after trying to omit the use of the "html" variable. Where did I go wrong?

script.js
var html = '';

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

2 Answers

Tiffany McAllister
Tiffany McAllister
25,806 Points

You have a syntax error in your for loop. You need to use semi-colons between each statement rather than commas.

You are also logging your html variable to the console outside of the for loop. You don't need the html variable. You just need to log the i variable to the console inside the for loop :)

Blake Atkerson
Blake Atkerson
2,797 Points

Thanks. That worked! I was just making it <em>more</em> complicated :/

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

There are two mistakes.

  1. Your initial html variable is a String type.

  2. You used comma two separate each stage of for loop instead of semi-colon.

Second one is simple syntax error so I'll go over first one.

What you will get after first iteration is String 4 not Number 4. '4' !== 4

From then the + operator is used as string concatenation rather than arithmetic addition. Because you are adding Number to a String. So your final result becomes something like '456...........156'

To fix this change your initial html value to Number and fix the syntax error in your for loop.