QUESTION IMAGE
Question
do this
play the game at least once. notice what is different about the end game screen.
discuss
look at lines 53-59. discuss with a partner how the mod operator % works.
modify
- it takes 5 lemons to make 1 cup of lemon juice. instead of telling the user if the number of lemons collected is even or odd, tell them:
- \you collected enough lemons for an exact number of cups of lemon juice\ if they collected a multiple of 5.
- \the amount of lemons you collected does not make an exact number of cups of lemon juice\ if they didnt collect a multiple of 5.
Analyze the current code logic
The current code in lines 53-59 uses the modulo operator % to check if the score is even or odd:
if (score % 2 == 0) {
setText("evenOddLabel", "You collected an EVEN number of lemons");
} else {
setText("evenOddLabel", "You collected an ODD number of lemons");
}
This checks divisibility by 2.
Identify the required modification
The prompt asks to modify the game so that it checks if the number of lemons collected (score) is a multiple of 5:
- It takes 5 lemons to make 1 cup of lemon juice.
- If
scoreis a multiple of 5 (an exact number of cups), display:"You collected enough lemons for an EXACT number of cups of lemon juice" - Otherwise, display:
"The amount of lemons you collected does NOT make an exact number of cups of lemon juice"
Apply conditional statements and modulo
Using Conditional Statements and Code Analysis, we determine that a number is a multiple of 5 if the remainder when divided by 5 is 0.
In JavaScript, this is written as score % 5 == 0.
Formulate the updated code block
We replace the condition score % 2 == 0 with score % 5 == 0 and update the text strings inside the setText function calls.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
To modify the program as requested, replace lines 55-59 in your workspace with the following code:
if (score % 5 == 0) {
setText("evenOddLabel", "You collected enough lemons for an EXACT number of cups of lemon juice");
} else {
setText("evenOddLabel", "The amount of lemons you collected does NOT make an exact number of cups of lemon juice");
}