0% found this document useful (0 votes)
5 views3 pages

Justine - Assignment 2

this is about cyber security

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Justine - Assignment 2

this is about cyber security

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 PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

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.

Answer:

START

DEFINE minimum = 1

DEFINE maximum = 100

SET hiddenNumber = randomly pick a number between minimum


and maximum

SET attemptCount = 0

PRINT "Let's play! Guess a number between", minimum, "and",


maximum

LOOP until the correct guess is made:

ASK player for a number guess

READ inputGuess

INCREMENT attemptCount by 1

IF inputGuess is less than hiddenNumber THEN


PRINT "Oops! That's too low."

ELSE IF inputGuess is greater than hiddenNumber THEN

PRINT "Nope! That’s too high."

ELSE

PRINT "Congratulations! You guessed it right in",


attemptCount, "tries."

END IF

END LOOP

END

2. 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.

ANSWER:

// Pick a number between 1 and 100 at random


const secretNumber = Math.floor(Math.random() * 100) + 1;
let guessCount = 0;
let userInput;

alert("Hello! I'm thinking of a number between 1 and 100.\nCan you


guess what it is?");
while (true) {
userInput = parseInt(prompt("Your guess:"));

// Validate that the input is a number


if (isNaN(userInput)) {
alert("That’s not a number! Please enter a valid number.");
continue;
}

guessCount++;

if (userInput < secretNumber) {


alert("Too low. Try a higher number.");
} else if (userInput > secretNumber) {
alert("Too high. Try a smaller number.");
} else {
alert(`You nailed it! It took you ${guessCount} attempts to find
the number.`);
break;
}
}

You might also like