Course F - Variables 1 - Variables With A Counter

Students create programs with events, variables, and conditionals.

  • Computer science
  • variables
  • events
  • conditionals
Print lesson
  • Leeftijd 10+
  • 90 min
  • JavaScript Blocks

Inleiding

This lesson introduces variables. A variable is a place to save information(numbers, text, etc) so we can grab it later. We use this saved information to show to people or make decisions. We will use variables in this lesson to create a counter.

The variables we use in this lesson will be numbers but in the second lesson of this series, 'VARIABLES AND EMOTIONS', we focus on text or string variables. We will use mathematical operators to change our variables.

One important thing to remember when using variables is to name your variables in a meaningful way. For example, if you want to save the number of basketballs your school has, you might name your variable 'basketballs' or 'number_of_basketballs'. However, a plain variable like 'x' is not recommended. Following this tip will make understanding your code and finding problems much easier.

How to make variables and name them is not explicitly covered in this lesson. It would be wise to demonstrate this before having students try this lesson on their own.

Our micro:bit course lessons are tailored to apply knowledge obtained from the Code.org CS Fundamentals. Before students begin these Course Lessons, we encourage students to first complete all CS Fundamentals as a prerequisite. Students should be familiar with variables from Code.org CS Fundamentals.

Students and teachers are highly encouraged to first complete the prerequisites in the resources section above.

Teacher Guide

Open

Activity

What you'll need

  • 1 - micro:bit
  • 1 - USB cable
  • 1 - AAA battery pack (optional)
  • 2 - AAA batteries (optional)

Documentation


basic.showNumber(1 - 1)
basic.showNumber(1 + 1)
basic.showNumber(1 * 1)
basic.showNumber(1 / 1)
            

Mathematical operators

Evaluate mathematical expressions


input.onButtonPressed(Button.A, () => {

})

            

onButtonPressed

Start an event handler (part of the program that will run when something happens, like when a button is pressed)


basic.showNumber(0)
            

showNumber

Show a number on the LED screen


let item = 0
item = 0
            

setItem

Use an equals sign to make a variable store the number or string you say.

4 steps

Guided Practice

We will make instructions or code online for the micro:bit. Next, go to: makecode.microbit.org


let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
        

We start our program with a variable called count. We initialize or start count by setting it to 0.

If you press button A, count will go up by 1.

If you press button B, count goes down by 1.

Follow the link to the starter code. There are extra blocks at the bottom to help you get started

The starter code is available here.

NOTE: The unused blocks at the bottom of the code are meant to be used in the challenges.

Challenges

The starter code is available here.

Challenge 1

Create a new variable, name it Adjustment, set it to 3 and put it in the on start block

View code

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
Adjustment = 3
          

Challenge 2

Instead of setting Count +/- 1, set Count to +/- Adjustment

View code

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
Adjustment = 3
          

Challenge 3

Use the A+B buttons pressed event handler to reset the count and then show it.

Hint: Use the extra blocks that were added to the code at the bottom.

View code

let Adjustment = 0
let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - Adjustment
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {
    count = 0
    basic.showNumber(count)
})
count = 0
Adjustment = 3
          

Independent Practice

Create your own variable on start. Change the variable in the on shake event. Be prepared to tell a story about this variable and why it changes.

Example - My variable is called maraca. When I shake it the variable goes down by 5. This shows how sad my mother gets when I play the maraca in her ear.

Quiz

Use these questions to reflect on the original code found here.

let count = 0
input.onButtonPressed(Button.A, () => {
    count = count + 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.B, () => {
    count = count - 1
    basic.showNumber(count)
})
input.onButtonPressed(Button.AB, () => {

})
count = 0
        

Question 1

What is a variable?

Show answer

Answers may vary but a variable is a place where you can store and retrieve data.

Question 2

What is the stored in the variable called count on start? Why are we creating the variable count in this code?

Show answer

count is 0 on start. The score changes when buttons are pressed so we need to store the value.

Question 3

Which number is stored in count after running this code and pressing button A once? Explain why you chose that number.

Show answer

We are only pressing button A once. The number on display is one. Pressing button A would add one to zero(1+0).

Question 4

Which number is stored in count after running this code, pressing button A one time, and pressing button B two times? Explain why you chose to that number.

Show answer

We are only pressing button A once. The number goes up 1. Then pressing button B twice subtracts 2. -1 will be on display.(0+1-2=-1)

Selecting this opens external content from our support system, which adheres to their privacy policy.