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

i just can't figure it out what am i doing wrong it says it only runs once

how do i fix it

script.js
var k = parseInt(0);

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

3 Answers

Grace Kelly
Grace Kelly
33,990 Points

You have the right idea, just a couple of minor things:

  1. This code challenge requires for you to log the number inside the for loop. You have done so outside the loop
  2. You don't need to increment your number inside the for loop as this is done already inside the parenthesis when you declare the loop

So looking at your code, we can make these small changes and ammend it like so:

for (var i = 4; i<= 156; i += 1 ) { //i += 1 increments i by one on each iteration of the loop
console.log(i) //log the number on each iteration of the loop
}

Making these small changes will make the code work :)

Hope that helps!!

Sam Baines
Sam Baines
4,315 Points

Hi Bas - its quite a simple mistake you have here - you need to make sure the console.log command is run within the loop not outside of it like you have it. Otherwise the loop runs for (i) through 157 times and then writes the value of (i) to the console. Code below:

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

Hope this helps.

the adjustments do make it work but i still says it writes to many numbers