Variables (numbers + strings)
Explore
- Set up 5 boxes and label them:
- favoriteNumber
- randomNumber
- age
- favoriteAnimal
- leastFavoriteAnimal
- Students write their personal answers for each of those subjects on small pieces of paper.
- Have one student volunteer to read their favorite number paper aloud and then drop it in the corresponding box.
- Ask students, “What is the value that is stored in favoriteNumber?”
- The value of favoriteNumber is whatever is on that student’s paper
- Continue until there is a paper in each of the boxes.
- Ask students what they think will happen if we want to change the value of favorite number.
- To change the value, take the old value out and drop the new value in.
- Change the values stored in the boxes.
- Ask students, “What is age + randomNumber?”
- The answer is the value that is stored inside the age box added to the value that is stored in the randomNumber box.
Suggested Questions:
- Why was it easier to know what the numbers stored in the variables meant?
- What do you think this is trying to tell us about giving instructions to computers?
Explain
Students follow along with the following example on repl.it.
Introduce variables
- A variable is a way to store data and give it a name so that it can be used later.
- Draw a box on the whiteboard. Explain that a variable is like a piece of paper that you can store a value on.
- Ask students, “What are some things that you think could be stored in a variable?”
- Students first write down the information they used in the previous activity. The goal is to keep each item to one line and come up with their own instruction conventions.
- Explain that humans can understand those instructions, but computers use their own language. Show how it’s done.
- To create a variable start with the keyword ‘var’ and then add a name
var myAge
Introduce numbers
- Now that there is a variable named myAge it can be given a value
- One type of value that can be stored is a number
- Students store their age in the variable myAge using the ‘=’
var myAge = 27 - To change the value of a variable once it has already been defined drop the keyword
varvar myAge = 27 myAge = 28 - You can also use math operations to change the value of a variable. Show students the example below and ask them what the value of myAge will be after the code is run. (29)
var myAge = 27 myAge = myAge + 2
Introduce strings
- The information that students have stored are numbers.
- Now students create a variable named favoriteAnimal and store the value from the explore phase.
var favoriteAnimal = dog - To save a word students need to add back tick (to the left of the one on the keyboard) marks. This tells the computer that the value is a word or a string.
var favoriteAnimal = `dog` - Students add a second variable for their leastFavoriteAnimal
- Variables can be added to strings by putting them inside of a pair of curly braces with a dollar sign in front.
${variable}var phrase = `My favorite animal is ${favoriteAnimal}`
Engage
- Split into smaller groups with tutors to complete variable worksheet. (http://bit.ly/1SebxsH)
- Messi goals
- Variable assignment debug