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

section 5 #45

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
68 changes: 34 additions & 34 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="index.css" rel="stylesheet" />
<title>Number Guesser</title>
</head>
<body>
<main>
<h1>Guessing Game</h1>
<p>
I'm thinking of a number from 1 to 99! Can you guess it in 5 tries or
less?

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="index.css" rel="stylesheet" />
<title>Number Guesser</title>
</head>

<body>
<main>
<h1>Guessing Game</h1>
<p>
I'm thinking of a number from 1 to 99! Can you guess it in 5 tries or
less?
</p>
<div>
<input type="number" id="guess" min="1" max="99" />
<button id="submit">Submit Guess</button>
</div>
<div>
<p class="message" id="number-of-guesses"></p>
<p class="message" id="too-high">You guessed too high. Try again.</p>
<p class="message" id="too-low">You guessed too low. Try again.</p>
<p class="message" id="correct">
Congratulations, You guessed correctly! <br />
Would you like to play again?
</p>
<div>
<input type="number" id="guess" min="1" max="99" />
<button id="submit">Submit Guess</button>
</div>
<div>
<p class="message" id="number-of-guesses"></p>
<p class="message" id="too-high">You guessed too high. Try again.</p>
<p class="message" id="too-low">You guessed too low. Try again.</p>
<p class="message" id="max-guesses">
You reached the max number of guesses
</p>
<p class="message" id="correct">
Congratulations, You guessed correctly! <br />
Would you like to play again?
</p>
</div>
<button id="reset">Reset</button>
</main>
<script src="index.js"></script>
</body>
</html>
</div>
<button id="reset">Reset</button>
</main>
<script src="index.js"></script>
</body>

</html>
71 changes: 46 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
const guessInput = document.getElementById('guess');
const submitButton = document.getElementById('submit');
const resetButton = document.getElementById('reset');
const messages = document.getElementsByClassName('message');
const numberOfGuessesMessage = document.getElementById('number-of-guesses');
const tooHighMessage = document.getElementById('too-high');
const tooLowMessage = document.getElementById('too-low');
const maxGuessesMessage = document.getElementById('max-guesses');
const numberOfGuessesMessage = document.getElementById('num-of-guesses');
const correctMessage = document.getElementById('correct');



let targetNumber;
const attempts = 0;
const maxNumberOfAttempts = 5;
let attempts = 0;
let maxNumberOfAttempts = 5;

// Returns a random number from min (inclusive) to max (exclusive)
// Usage:
// > getRandomNumber(1, 50)
// <- 32
// > getRandomNumber(1, 50)
// <- 11


function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

function checkGuess() {
// Get value from guess input element
const guess = parseInt(guessInput.value, 10);
attempts = attempts + 1;

if (guess < 1 || guess > 99) {
alert('Number must be greater than 0 and less than 100. Try Again.');
return;
}

attempts++;

hideAllMessages();

Expand All @@ -35,49 +44,60 @@ function checkGuess() {

correctMessage.style.display = '';

if (attempts === 1) {
correctMessage.innerHTML = 'Congratulations, You guessed correctly! <br/> Would like to play again?';
}

submitButton.disabled = true;
guessInput.disabled = true;
}

if (guess !== targetNumber) {
resetButton.style.display = '';
} else {
if (guess < targetNumber) {
tooLowMessage.style.display = '';
} else {
tooLowMessage.style.display = '';
tooHighMessage.style.display = '';
}

const remainingAttempts = maxNumberOfAttempts - attempts;

numberOfGuessesMessage.style.display = '';
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`;
}
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${maxNumberOfAttempts - attempts} guesses remaining`;

if (attempts ==== maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
if (attempts === maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
numberOfGuessesMessage.innerHTML = '0 guesses remaining';
resetButton.style.display = '';
}
}

guessInput.value = '';

resetButton.style.display = '';
}

function hideAllMessages() {
for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) {
messages[elementIndex].style.display = 'none';
}
const messages = [
numberOfGuessesMessage,
tooHighMessage,
tooLowMessage,
maxGuessesMessage,
correctMessage,
];

messages.forEach((message) => {
message.style.display = 'none';
});
}

funtion setup() {
function setup() {
// Get random number
targetNumber = getRandomNumber(1, 100);
targetNumber = getRandomNumber(1, 99);
//test when correct
//targetNumber = 42;
console.log(`target number: ${targetNumber}`);

// Reset number of attempts
maxNumberOfAttempts = 0;
attempts = 0;

// Enable the input and submit button
submitButton.disabeld = false;
submitButton.disabled = false;
guessInput.disabled = false;

hideAllMessages();
Expand All @@ -88,3 +108,4 @@ submitButton.addEventListener('click', checkGuess);
resetButton.addEventListener('click', setup);

setup();