FAQ: Variables - Changing the Value of a Variable

#1

This community-built FAQ covers the "Changing the Value of a Variable " exercise from the lesson “Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise _Changing the Value of a Variable _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

where is the drop down menu to change the game options located

1 Like
#3

It says the value of a variable can be changed part way through the code. You are using the example “my_number” in this case. How does the computer not get confused over which value belongs to the variable name as it is the same for both the old and the new number?

#4

The latest assignment takes the place of the earlier one. The value first referenced is no longer in effect. my_number will now refer to new_number, as it were.

It’s very common to have variables change their values. In fact that is why we refer to them as variable. They are subject to change. The only ones that cannot be changed are constants.

const pi = 3.1415;
#5

Interesting. I get it!
A follow up question then:

  • Isnt it better to “clean up” the code then and remove old variabler no longer needed or used?

Thanks for your help

2 Likes
#6

Programs are meant to be dynamic, not static. We want to build in as much flexibility as possible so the code may be re-used.

function times_2_plus_3()  {
    return 5 * 2 + 3;
}

The above is a static function. It contains no variables, only literals and returns a constant value, 13. How can we make it dynamic and reusable? By letting the user input any value, not just 5.

function times_2_plus_3(number)  {
    return number * 2 + 3;
}

Now can reuse this function on any number, which will be doubled and added to 3.

Cleaning up the code only means removing redundancy, repetition and simplifying bloated code patterns. It does not mean manually reseting values on variables. The program needs to be able to do that on the fly in the normal course of action.

Perhaps you could post the code sample in question? That will give us something to bounce this discussion off of.

1 Like