Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create 22exercise1 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Create 22exercise1 #3

wants to merge 1 commit into from

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.

None yet

1 participant