Skip to content

Create 22exercise1#3

Open
zeel203 wants to merge 1 commit intoCodeWithHarry:mainfrom
zeel203:patch-1
Open

Create 22exercise1#3
zeel203 wants to merge 1 commit intoCodeWithHarry:mainfrom
zeel203:patch-1

Conversation

@zeel203
Copy link

@zeel203 zeel203 commented Jan 21, 2025

// Exercise 1: Guess the number
// Prize Amount: Rs 500 Amazon voucher

// Function to generate a random number between 1 and 100
let number = Math.floor(Math.random() * 100) + 1;

let chances = 0;
let guessedNumber = 0;

// Keep asking the user for the number until the correct number is guessed
while (guessedNumber !== number) {
// Ask user for their guess
guessedNumber = parseInt(prompt("Guess the number (between 1 and 100): "), 10);

// Increment the chances
chances++;

// Provide feedback to the user
if (guessedNumber < number) {
    console.log("The number is higher. Try again!");
} else if (guessedNumber > number) {
    console.log("The number is lower. Try again!");
}

}

// Game over, correct number is guessed
console.log(Congratulations! You've guessed the number in ${chances} chances.);
console.log(Your score is: ${100 - chances});
console.log(The actual number was: ${number});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant