Improvement needed? (Increment a Number with Javascript)

Improvement needed? (Increment a Number with Javascript)
0

#1

This lessons mentions, in passing, the general form for assigning a new value to a variable based on its existing value:

i++ is the equivalent of i = i + 1

I.e., the form of variable = variable <operator> <expression>

Should this technique be explicitly explained, either in this lesson or a prior one? I only ask because when I first learned this paradigm many years ago it seemed pretty strange at the time. How can I have the same variable on both sides of the equals sign? Doesn’t that create some kind of weird infinite loop? Such were my questions back then :slight_smile:

Anyway, what do you guys think? Did any of you trip up on this when you were learning?

S~


#2

Aha! There is a lesson on this a little later on:

:smiley:

Still, it might be confusing to make reference to a technique that hasn’t been explained yet. Perhaps the former lesson can simply state that i++ is the way to increment a number, and then explain in the latter lesson that it’s actually a short cut? Or, as most programming tutorials do, perhaps you could move the increment/decrement lessons to appear after the one that covers modification assignments.


#3

Hey stewartmurrie,

I recommend opening up an issue here https://github.com/FreeCodeCamp/FreeCodeCamp/issues . If the moderators agree with you and implements your suggestion, you just made FCC incrementally better! :slight_smile:

If you’re really ambitious, if the the moderator agrees with you, you can do a pull request.

Joseph


#4

Hi @joechan3! Thanks for the advice. The FCC contrib.md says to wait for 3rd party confirmation before entering a new bug, so that’s why I posted here first, hoping to find someone else who agreed :slight_smile: But I suppose there’s no harm in creating the issue and having the discussion in Github directly.


#5

Ah, I stand corrected. Good to know for next time I submit an issue.


#6

The way I view it and the way I think JavaScript interprets it, is kind of like

variable = "value of variable"+ 1

the equals symbol creates that distinction. if the variable = 1 you aren’t saying 1 = 1+1, you’re saying the 'variable which already has a value of 1 is NOW equal it’s current value + 1.


#7

I’d say, because the right-hand side is read first and (almost) everything is an object, if you were to write

num = num + 1```

it would be interpreted like

```num = {type: Number, value: 1}
temp = num.value + 1
num.value = temp```

#8

I always found i++ much more confusing than i = i + 1. Especially since in other languages there’s also ++i (try to reason about this one and its difference to i++!).

Compared to that, i = i + 1 seems very intuitive to me.

More fun are the compound assignment operators, like i += 1 or even i **= 2


#9

Not just other languages. JavaScript itself has i++ AND ++i.


#10

Jesus… and I thought I had escaped it when I started doing Javascript, I never actually see anyone use ++i now, while I saw it used in Java and C-blabla all the time.