Interactive Guessing Game
Overview and Objectives
In this lab, learners will apply control flow concepts in JavaScript by creating an interactive guessing game. This exercise reinforces the use of conditional execution (if/else statements) and loops (for/while) in a practical scenario. By the end of this lab, learners will be able to:
- Use loops to repeat actions based on user input.
- Implement conditional statements to compare values and control the game flow.
- Use JavaScript functions such as
prompt()
for user input andalert()
orconsole.log()
for output. - Handle basic input validation and provide appropriate feedback.
Tasks and Instructions
Task 1: Develop the Guessing Game Script
-
File Creation:
- Create a new JavaScript file named
guessingGame.js
.
- Create a new JavaScript file named
-
Generate a Secret Number:
- Use JavaScript’s
Math.random()
andMath.floor()
functions to generate a secret number between 1 and 10. For example:const secretNumber = Math.floor(Math.random() * 10) + 1;
- Use JavaScript’s
-
User Interaction and Looping:
- Use a loop (
while
orfor
) to allow the user up to 3 attempts to guess the secret number. - Inside the loop:
- Use
prompt()
to ask the user for their guess. - Convert the input (which is a string) to a number.
- Use conditional statements (
if...else if...else
) to compare the guess with the secret number:- If the guess is equal to the secret number, display a congratulatory message (using
alert()
orconsole.log()
) and exit the loop. - If the guess is higher than the secret number, inform the user that their guess is too high.
- If the guess is lower than the secret number, inform the user that their guess is too low.
- If the guess is equal to the secret number, display a congratulatory message (using
- Use
- After 3 incorrect attempts, display a message informing the user that they have lost and reveal the secret number.
- Use a loop (
-
Enhancements:
- Allow the user to play again by asking if they would like to retry the game.
- Include input validation to ensure that the user enters a number. If not, prompt the user again or show an error message.
Task 2: Test Your Script
- Execution:
- Run your script by including it in an HTML file and opening it in your browser, or by using an online JavaScript editor.
- Verification:
- Verify that the game functions as expected: valid guesses yield appropriate feedback, correct guesses end the game immediately, and after 3 attempts the game reveals the secret number.
Submission Criteria
The following should be included within a GitHub repository, and submitted via Canvas:
- Files/Links:
- Submit the
guessingGame.js
file. - If using an online editor (e.g., CodePen, JSFiddle), include a link to your project.
- Submit the
- Evidence of Output:
- Provide a screenshot or text log demonstrating game execution, including at least one complete playthrough (both win and loss scenarios if possible).
- Error-Free Code:
- Ensure that the script executes without syntax or runtime errors.
Grading Rubric
Criteria | Points | Description |
---|---|---|
Secret Number Generation | 10 pts | The code correctly generates a random number between 1 and 10 using Math.random() and Math.floor() . |
Loop Structure | 10 pts | Implements a loop (for/while) to allow up to 3 attempts; loop logic is correct and terminates under appropriate conditions. |
User Input and Conversion | 10 pts | Uses prompt() to obtain user input and correctly converts the input from string to a number for comparison. |
Conditional Logic and Feedback | 15 pts | Uses if/else if/else statements to compare the guess with the secret number, providing correct and clear feedback (too high, too low, or correct). |
Handling Maximum Attempts | 5 pts | Properly handles the scenario where the user exhausts 3 attempts by informing the user and revealing the secret number. |
Enhancements | 5 pts | Credit for additional features such as replay functionality or improved input validation; points added only if fully functional. |
Code Quality and Organization | 5 pts | Code is neatly formatted with descriptive variable names, appropriate comments, and logical structure. |
Total Points: 60 points
Partial credit will be awarded for submissions that demonstrate a sound understanding of control flow in JavaScript even if some components are incomplete or contain minor errors. For instance, if the loop structure and conditional logic are correctly implemented but there is a minor issue with input conversion, points will be deducted proportionally. Credit will be given for clear use of loops and conditionals, as well as for attempts at optional enhancements, even if they are not fully realized.