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

Dana Holdt
Dana Holdt
5,578 Points

Need help with challenge: Creating a 'for' loop that prints 4 - 156?

I am a bit confused as to how this isn't functioning properly. If I change the start number to '3' the individual numbers between 3 to 156 are printed to the log and the challenge yells at me saying the printed numbers must be between 4 and 156. However, if I change the starting number to '4' I get an error stating 'The console.log needs to print the individual numbers from 4 to 156 to complete the loop'.

script.js
var num = ' ';

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

2 Answers

Karolin Rafalski
Karolin Rafalski
11,368 Points

You are adding a space to each number. The challenge only wants you to print the number.

for (var i = 4; i <=156; i++){
  console.log(i);
}
Dana Holdt
Dana Holdt
5,578 Points

Ahhh... Thanks, I'll try that. One quick question about your answer: The ' i++ ' ? I haven't encountered that before? I'm going to guess it's another useful shorthand?

Pavithra Manivannan
Pavithra Manivannan
7,474 Points

You put a space between console.log and the argument num. try this. var num = ' ';

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