0% found this document useful (0 votes)
0 views

Programming Assignment 2

this is about programming

Uploaded by

ecowbadwemoore76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming Assignment 2

this is about programming

Uploaded by

ecowbadwemoore76
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 2 (10 Marks Total)

1. (5 Marks) Design an Algorithm

 Scenario: You are creating a simple text-based game where the


player needs to guess a randomly generated number within a
certain range (e.g., 1-100).
 Task:
o Design an algorithm (using pseudocode or a flowchart) for
this game.
o Your algorithm should:
 Generate a random number within the specified range.
 Prompt the player to enter their guess.
 Provide feedback to the player (e.g., "Too high", "Too
low", "Correct!").
 Allow the player to continue guessing until they guess
the correct number.
 Keep track of the number of attempts.

2. (5 Marks) Code Implementation

 Task:
o Implement the algorithm you designed in Question 1 using
Javascript
o Your code should:
 Generate a random number.
 Prompt the user for input.
 Provide feedback to the user based on their guess.
 Keep track of the number of attempts.
 Display the number of attempts taken by the player to
guess the correct number.

1. BEGIN
SET lower_limit = 1
SET upper_limit = 100
GENERATE secret_number = RANDOM_NUMBER between
lower_limit and upper_limit
SET attempts = 0

DISPLAY "Welcome to the Number Guessing Game!"


DISPLAY "Guess a number between", lower_limit, "and", upper_limit

REPEAT
PROMPT "Enter your guess: "
READ guess
INCREMENT attempts by 1

IF guess < secret_number THEN


DISPLAY "Too low!"
ELSE IF guess > secret_number THEN
DISPLAY "Too high!"
ELSE
DISPLAY "Correct! You guessed the number in", attempts,
"attempts."
END IF

UNTIL guess == secret_number


END

2. // Generate a random number between 1 and 100


3. const secretNumber = Math.floor(Math.random() * 100) + 1;
4. let guess;
5. let attempts = 0;
6.
7. alert("Welcome to the Number Guessing Game!\nGuess a number
between 1 and 100.");
8.
9. while (true) {
10. guess = parseInt(prompt("Enter your guess:"));
11.
12. // Check if input is a valid number
13. if (isNaN(guess)) {
14. alert("Please enter a valid number.");
15. continue;
16. }
17.
18. attempts++;
19.
20. if (guess < secretNumber) {
21. alert("Too low!");
22. } else if (guess > secretNumber) {
23. alert("Too high!");
24. } else {
25. alert(`Correct! You guessed the number in ${attempts}
attempts.`);
26. break;
27. }
28. }
29.

You might also like