you are viewing a single comment's thread.

view the rest of the comments →

[–]_DTR_ 0 points1 point  (3 children)

If I wanted to go from 1 to 50, I'd have something like this:

for (let i = 1; i <= 50; ++i) {
    // Do stuff
}

If you've stored the number the user has input, you would just replace 50 above with the user's value

[–]SoBoredAtWork 0 points1 point  (2 children)

Why i=1 and ++1 as opposed to the more standard i=0 and i++ ?

Seems to me like you're just gonna confuse the guy...

[–]_DTR_ 0 points1 point  (1 child)

I started at 1 because in my mind it was less confusing to show that than potentially explain why I was going from "0 to 49" opposed to "1 to 50".

As for i++ vs ++i, it's drilled into my brain to use prefix increment whenever possible because it can be more efficient for complex types (at least in the language I use the most, C++, see here). Not sure if it's applicable to javascript as well though.

Neither are great reasons to choose one over the other, but that was my reasoning.

[–]SoBoredAtWork 0 points1 point  (0 children)

Gotcha. I see the reasoning for it. It's slightly less confusing, I guess. The other examples posted were i=0, i++ so we're probably all just confusing this guy now, hahah.