Microbit JavaScript
Intermediate
60 mins
Teacher/Student led
230 points
What you need:
Chromebook/Laptop/PC

JavaScript - Exactly 11

In this step-by-step lesson, you'll create a game using JavaScript, where the goal is to guess when exactly 11 seconds have passed. You'll learn how to use variables, functions, and mathematical operations, and apply these skills to create a fun, interactive game. This lesson involves creating and manipulating variables, setting start and end times, calculating differences, and displaying results.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Start a New Microbit Project

    In this lesson, you'll be creating an exciting game using your JavaScript skills. The game is called 'Exactly 11', and the goal is to guess when exactly 11 seconds have passed. This project will help you understand how to use variables, functions, and mathematical operations in JavaScript. Let's get started!

    To begin, navigate to the MakeCode Microbit editor by clicking on this link: MakeCode Microbit. Once you're there, start a new project by clicking on 'New Project' in the 'My Projects' section. Give your project a name, for instance, 'Exactly 11'.

    2 - Create a 'starttime' variable

    In the game the player will try and guess when 11 seconds has passed. We will need a variable to remember the start time to create a variable called 'starttime' by adding the following code.

    let starttime = 0

    This code creates a variable called 'starttime' and sets it's value to 0.

    3 - Set the start time

    Now add the following new code to remember when the clock is started and to show an icon to let the user know the clock has started.

    let starttime = 0
    
    // add this code
    input.onButtonPressed(Button.A, function () {
        starttime = input.runningTime()
        basic.showIcon(IconNames.SmallDiamond)
    })

    The input.runningTime() code returns the number of milliseconds since the program started. (One second is 1000 milliseconds).

    Note that in JavaScript when you put // in the code, the computer treats this line as a comment and not actually code that it needs to run.

    // this a comment
    let firstname = 'Sophie'
    
    let lastname = 'Jones' // this is another comment
    

    Putting comments in your code is a good idea as it gives some extra information to anyone looking at your code and helps them understand it better.



    4 - Create a 'taken' variable

    Now create a variable called 'taken', we are going to use this to store the amount of time it has taken between pressing A and pressing B.

    Add the new code to your project.

    let starttime = 0
    let taken = 0 // add this new code
    
    input.onButtonPressed(Button.A, function () {
        starttime = input.runningTime()
        basic.showIcon(IconNames.SmallDiamond)    
    })

    5 - Set the taken time

    Now let's program the B button to set the 'taken' by subtracting the 'starttime' from the current running time.

    Add the following new code.

    let starttime = 0
    let taken = 0
    
    input.onButtonPressed(Button.A, function () {
        starttime = input.runningTime()
        basic.showIcon(IconNames.SmallDiamond)    
    })
    
    // add this code
    input.onButtonPressed(Button.B, function () {
        taken = input.runningTime() - starttime
    })

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more