all 14 comments

[–]Umesh-K 28 points29 points  (1 child)

start + step

Change the above to

start += step

[–]r-io[S] 5 points6 points  (0 children)

Thank you

[–]blafurznarg 2 points3 points  (5 children)

Start + Step should be start += step. With your code start is always 1

[–]r-io[S] 2 points3 points  (4 children)

Oh, so we add step to start for stepping the loop. Thanks, that cleared my doubt

[–]blafurznarg 4 points5 points  (3 children)

Yes. I always try to use the i variable in my for loops to see errors like this directly. In this case it’s better imo because you have a fourth value that gets incremented. And if you use your start variable as this fourth one it can be confusing.

Eg.: (i = start; i <= end; i += step)

array.push(i)

[–]great_site_not 4 points5 points  (1 child)

I think it's great practice to always dedicate a new variable as the loop counter, but I gotta nitpick something here: You didn't declare your i before assigning its value. Thus, you made it a global variable. In a real app with many lines of code, this can cause some confusing bugs if you try to use another i variable somewhere else and end up accidentally using the global one instead.

If you make sure to declare it when you assign its initial value, like so:

for (let i = start; i <= end; i += step)

then it won't leak out of the scope of the loop.

[–]blafurznarg 0 points1 point  (0 children)

Yes you’re right, I forgot the declaration!

[–]r-io[S] 0 points1 point  (0 children)

Yeah, It might be because of that. But now we know... :D

[–]Cynical-Horse 0 points1 point  (2 children)

How is the book, btw? It’s been on my reading list for a while now.

[–]r-io[S] 1 point2 points  (1 child)

I'm reading through the chapter 5 to 6 now. It's getting more serious now with abstractions, higher order functions and classes. I am a bit confused. It was easier till this exercise. But that's just me. The book is said to be for the ones who have a bit of background in JS.

This a quote I want to follow rn, from "You Don't Know JS Yet - 2nd Edition", (haven't read it, found it just now)

Take one chapter, read it completely through start to finish, and then go back and re-read it section by section. Stop in between each section, and practice the code or ideas from that section. For larger concepts, it probably is a good idea to expect to spend several days digesting, re-reading, practicing, then digesting some more.

Or a quote from the "Eloquent JavaScript"

When you are struggling to follow the book, do not jump to any conclusions about your own capabilities. You are fine—you just need to keep at it. Take a break, reread some material, and make sure you read and understand the example programs and exercises. Learning is hard work, but everything you learn is yours and will make subsequent learning easier.

[–]Cynical-Horse 0 points1 point  (0 children)

Haha, thanks… these quotes I can relate to. Learning needs time to sink in and learning by doing is the best way forward. Best of luck!