Check user guess
Learning Goals
By the end of the lesson, students will be able to:
- write simple conditionals
- return values based on input
- use conditionals to check if the user won in their guess the number game
Overview
This lesson introduces students to the process of adding new features to their projects. Start by leading a pseudocode planning time. Once the concepts required have been outlined, introduce students to conditionals using the conditionals lesson. After practicing conditionals in their smaller projects, refocus students on guess the number project. Students get with a partner and discuss strategies for implementing conditionals in their project to check if the player has guessed the correct number.
Suggested stations
- String templates
- Function syntax
plan (5 minutes)
- Lead a discussion with students about how the guessing part of the game would work in the human context. The final solution should look similar to:
- Person 2 guesses a number.
- Person 1 (game) checks if the guess is correct. (conditionals)
- If the guess is correct person 2 wins (return, strings)
- Otherwise person the guess is incorrect
- Label each of the steps with a javascript concept (in the parentheses above)
- Discuss which concepts are new.
discover (30-45 minutes)
produce (30 minutes)
pseudocode
FUNCTION main (guess)
IF guess is equal to answer
RETURN win message
ELSE
RETURN lose message
With their partner, students plan and attempt to implement a solution for checking if the user guess is correct.
- Fill in handout
- Discuss plans
- Attempt a solution
- Justify reasoning
implement (15 minutes)
- Discuss with students their ideas. Make sure to have students from each group explain what they attempted or any ideas they had on how they could use their knowledge to implement a solution.
- After hearing their solutions, introduce students to the teacher solution.
function main (guess) {
return guessNumber(guess)
}
function guessNumber (guess) {
if (checkGuess(guess)) {
return `You win!` // or any winning message
} else {
return `Nope!` // or any losing message
}
}
function checkGuess (guess) {
if (guess === 4) { // could be any number
return true
} else {
return false
}
}
- Students contrast the teacher solution with their own ideas.
- Student implement a working solution in their project.
- Students add comments to their code explaining what each line means.
justify (5 minutes)
- As a group, explain the code that they have written. Make sure to decompose each block and expression that was added to the code.
- Tell students that in the future, they are expected to justify their code in this way after each feature that they add.
- Students practice by explaining their code to a partner.
test
- Students play the game of their partner.
- When students are testing a game, they are looking for:
- bugs
- feature improvements
- When students find a bug they fill out a bug report:
- What were you doing in the game?
- What did you type into the input box?
- What did you expect to happen?
- What actually happened?