← Back to all posts
JS Assignment 5: For Loops

## What am I doing wrong here?

function exerciseTwo(){
let count = 0;
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for( let count = 0; count <= 10; count++)
return count;
}

Answered by heyitsmarcus (306) [earned 5 cycles]
heyitsmarcus (306)

You should remove the first `let count = 0` from the `for` loop. This is not necessary for your algorithm to complete since you've instantiated `count` in the beginning of the function. And using `let count = 0` inside of the `for` loop instantiates a new variable called count within the `for` loop, which isn't what you want. After the loop completes, the `count` that gets returned (the very first `let count = 0;`) is still `0`.

Also, get rid of the `<=` because that will cause the loop to run 11 times and return an 11 since it's going from 0 to 10:

``````function exerciseTwo(){
let count = 0;
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for(; count < 10; count++) {}
return count;
}``````
heyitsmarcus (306)

@heyitsmarcus A more standard approach, as well, might be:

``````function exerciseTwo(){
let count = 0;
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for(count = 0; count < 10; count++) {}
return count;
}``````

Although, again, it isn't necessary to leave `count = 0` inside the `for` loop, since you aren't manipulating its value elsewhere in the function.

Vandesm14 (2207)

@heyitsmarcus A more modern approach:

``````function exerciseTwo(){
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for(let i = 0; i < 10; i++) {}

// return i; // This will end the function right then and there, so I will rewrite it:
console.log(i);
}``````
heyitsmarcus (306)

@Vandesm14 This doesn't work. `i` is created within the `for` loop and then doesn't exist outside of the `for` loop, so you will receive a `Reference error` in the console when you look at this in the developer console.

My approach is a modern approach, thank you.

heyitsmarcus (306)

@Vandesm14 With that being said, you could use hoisting in ECMAScript 5, and use your approach (change `let` to `var`). By changing from `let` to `var` the `i` variable is hoisted into the function scope and is then available to `return` and `console.log`.

``````function exerciseTwo(){
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for(var i = 0; i < 10; i++) {}

// return i; // This will end the function right then and there, so I will rewrite it:
console.log(i);
}``````
JerehlDyer (4)

@heyitsmarcus Thank you! Also how do I know when to put the {} at the end?

heyitsmarcus (306)

@JerehlDyer Typically, you wouldn't do as I did there. That was done to satisfy the algorithm. We had to put those curly braces in for the `for` loop because if we didn't, JavaScript determines that the line after any kind of loop such as `for` or a conditional such as `if` belongs with the `for` or `if` or whatever loop/conditional it might be.

As an example, let's say you have this function:

``````function a() {
let count = 0;
for (count = 0; count < 10; count++)
count++;
return count;
}``````

I formatted it a bit weirdly on purpose. And this is why I always use brackets. With the way this is formatted, you'd assume that the `for` loop executes just once, to add 1 to `count`, and then returns the `count` variable. What actually happens is that only the very next line counts as part of the `for` loop (the `count++`). The line `return count` executes after the for loop.

A better formatted example that is easier to read and understand:

``````function a() {
let count = 0;
for (count = 0; count < 10; count++) {
count++;
}

return count;
}``````

Does that make sense?

JerehlDyer (4)

@heyitsmarcus Yeah kinda, so in the curly brackets your saying it runs count++ over again. I also saw your password generator, pretty cool 😎. Are you self taught?

heyitsmarcus (306)

@JerehlDyer I'm completely self taught. I've been programming, though, since I was about 14 years old. I work as a Full Stack developer for a marketing company. I take little breaks and help answer questions on here and Stack Overflow. Keep going and keep learning and you can end up in a development job if you so wish!

heyitsmarcus (306)

@JerehlDyer Oh and the password generator was just something I helped someone else with haha But thanks! I have a website: http://www.marcusparsons.com where you can see some of my JavaScript projects.

JerehlDyer (4)

@heyitsmarcus That's the goal! Since you're self-taught where did you get your information?

JerehlDyer (4)

@heyitsmarcus Impressive stuff! Love the games and apps.

heyitsmarcus (306)

@JerehlDyer I have to highly recommend FreeCodeCamp. I go through it myself just to keep myself sharp on web development. It provides a great, interactive, and totally free experience for learning web development. The MDN (Mozilla Developer Network) has fantastic references for specific Web technologies when you can't remember what something does or what all methods Arrays have lol. There are tons of great resources out there. But, FreeCodeCamp will get you a HUGE jump start.

JerehlDyer (4)

@heyitsmarcus Thank you and what's your opinion on lambda.com?

heyitsmarcus (306)

@JerehlDyer Did you mean lambdaschool.com? If so, I've never used it before. I've never had the opportunity to be able to use it, so I wouldn't be a good resource to ask about it.

patrickvmederos (1)

i dont understand. i watched the video posted at least 4 times and have no idea how i was supposed to figure this out without googling it.

edit: even after reading everything you guys took the time to write here, I still don't understand this at all. I am struggling significantly with learning this material, does anyone else find this very challenging? To me it seems like to everyone else in the world this stuff is common sense and a breeze to learn

ibedave777 (1)

This worked for me.

function exerciseTwo(){
let count = 0;
// In this exercise write your own for loop (you can look at the syntax above).
// It should loop 10 times.
// You are given a variable called: count .
// For each loop reassign count to the current value of count + 1 .
for(count; count <10; count++) {

}

kurrycat2004 (10)

I think its because of that `return count;`.
Return returns in your case the var count but it exits also the function. So the for loop would only run one time and then cause of that return it will exit.

heyitsmarcus (306)

@kurrycat2004 That is one of the problems. Since it's the next line of execution (and there are no curly braces), it is running inside the `for` loop and immediately returns once the first loop is entered into. But, there's also the problem of `let count = 0` inside the `for` loop. You can see my answer for a more detailed explanation of what all is happening inside this function.

mwilki7 (938)

It looks like you're using two definitions of "count"
`let count = 0;` on the 2nd line
and
`for( let count = 0;..)` on the 7th line

using the word 'let' in a different pair of brackets (for loop brackets in this case) will give you a brand new "count" and the old count doesnt get changed

So try removing the word "let" in the for loop and see if that does anything different.

Sobeshot (0)

@mwilki7 This is exactly what I was doing wrong! thank you for the tip, such a simple thing to overlook.