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;
}

You are viewing a single comment. View All
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.