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

The For loop function, where am I going wrong?

for (var i =4; <=156;i+=1) {

retrun i;

}

console.log(parseInt(i));

script.js
for (var i = 4;<=156;i+=1) 
{
return i;
}
console.log(parseInt(i) );

3 Answers

That is brill. Your answers are very easy to understand so you have made a friend for life now! Thanks once again. I can't wait to be coding properly I have so many ideas for apps etc....

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I think you're doing pretty well here, but you've got a couple of issues that are hindering your progress. First, your for loop is set up incorrectly.

Take a look:

// You typed
for (var i = 4;<=156;i+=1) 

// You meant to type
for (var i = 4;i<=156;i+=1)   // note the  i <= 156 as opposed to just <= 156

Secondly, the return statement inside your loop will be considered a syntax error as an Illegal return statement. Your console.log(i) is meant to be inside the for loop. A return statement is used from within a function, and your loop is not contained inside a function.

Hope this helps! :sparkles:

Thanks it makes sense but I'm not sure now how to make console log () display i 156, I thought that would be covered by the brackets, could you please share with me how that is done too?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Simply erase the last line of your code and where you have return i;, replace it with console.log(i); The for loop starts with i = 4, so the first iteration 4 will be output to the console. Then we increment i by one so it will now be equal to 5. The next iteration "5" will show up in the console. This continues until it hits 156 at which point console.log(i) will be logging out 156 and the for loop will stop executing.

I cannot recommend enough that you either run this in your browser's console or in a workspace so that you can see what this code is doing. Until then, it's likely that this explanation will feel very abstract to you.

Hope this helps! :sparkles: