Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStephanie Fu
1,939 PointsAt 2:06, why is example += 1 stored in res1 instead of example?
The variable example was stored back into itself when 1 was added to it the first time. Why is it stored in res1 after adding 1 to it again?
1 Answer
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello Stephanie,
Far be it from me to speak on behalf of Craig.
But I think he was wanting to keep example the same and not change it's value and instead store them in other variables so that he could show things such as post increment, and pre-increment and what the difference between them was.
This way he could show people how to increment and decrements without having to reset the example back to it's initial value after each demonstration of increment or decrementing the integer.
Stephanie Fu
1,939 PointsStephanie Fu
1,939 PointsThat makes sense from a Craig point of view, but looking at the code, at 1:33, the REPL returns example as 2. Why, then, at 2:00, is 3 stored in res1 instead of example? They're basically the same thing (example = example + 1 and example++), why are they stored in 2 different variables?
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsRob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsI would assume because Craig still wanted to to keep the variables different after teaching the basic foundations of incrementing and post incrementing.
He'd be the one to know the real reason of it, but safe to assume he did it to keep easy track of what topics the increment and decrement logic that he was trying to show throughout multiple variables. So you we can see the difference between post incrementing like example++ and ++example.