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 trial

JavaScript JavaScript Basics (Retired) Working With Numbers The Mad Libs Challenge Revisited

Lia Seltene
Lia Seltene
2,641 Points

You have to update variables if it contains another variable that has been modified??

I just wanna be clear.

If you have this:

var candyAmt = 10;
var yum = "I have " + candyAmt + " pieces of skittles";

and the variable candyAmt is modified

candyAmt -= 2;

you have to define any variable (that you continue to use) containing candyAmt, again??

yum = "I have " + candyAmt + " pieces of skittles.";
var response = "Dion gave away 2 skittles and said, \"Now, " + yum + "\"" ;

Is that right?? Variables won't update itself within another variable, just as standalone?

3 Answers

Ignazio Calo
PLUS
Ignazio Calo
Courses Plus Student 1,819 Points

I see your question, and the answer is "no" the other variable is not auto-updated.

To put this concept in a better way, the moment when you write:

var yum = "I have " + candyAmt + " pieces of skittles";

a variable called yum is defined, the the computer looks at the right part of the assignment and start building that string, taking I have, then checking the current value of the variable candyAmt and appending to the first part, and at the end appending pieces of skittles. When the whole expression is completed, the result is finally inserted into the yum.

At that point there is not information anymore that the string was built using the candyAmt variable. It's just a string.

In other words, if you have:

var total = 100 + 100

The sum is calculated only once and the value is stored into the total variable, is not re-evaluated every time.

Ignazio Calo
PLUS
Ignazio Calo
Courses Plus Student 1,819 Points

yep the first one is 180, the second one 160.

A more clean way to obtain the same result is do not use a variable, but use a function instead.

var num = 10;

var total = function(param) {
    return param * 2
};

console.log(total(num)); //20

num += 10
console.log(total(num)); //40

num += 10
console.log(total(num)); //60

In this way we don't need to "update" the total variable, because is a function, and it's evaluated every time.

Lia Seltene
Lia Seltene
2,641 Points

Okay, thank you! I think that's what we will start learning next. I think I was alarmed because it seemed counterintuitive, but having functions like you showed and "loops" coming up makes sense. I appreciate it.

Lia Seltene
Lia Seltene
2,641 Points

Ohhh okay. It gets evaluated once, and if you want it to say something different but keep the other variable within it for some reason, you do have to start over kinda?

so with that example

var num = 10;
var total = 100 - num;

num = 20;
var x = 2;

total *= x;

That will make the total = 180 and not 160. But if I do:

var num = 10;
var total = 100 - num;

num = 20;
var x = 2;

total = 100 - num;
total *= x;

Then the total will be 160. Is that right?