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

completed debugging in the html and javascript files #1

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1>Guessing Game</h1>
less?
</p>
<div>
<input type="number" id="guess" min="1" max="99" />
<input type="number" id="guess" min='1' max='99' />
<button id="submit">Submit Guess</button>
</div>
<div>
Expand Down
41 changes: 28 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ const messages = document.getElementsByClassName('message');
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 numberOfGuessesMessage = document.getElementById('number-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:
Expand All @@ -22,18 +22,22 @@ 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);
let guess = parseInt(guessInput.value, 10);
attempts = attempts + 1;

hideAllMessages();


if (guess === targetNumber) {
numberOfGuessesMessage.style.display = '';
numberOfGuessesMessage.innerHTML = `You made ${attempts} guesses`;

correctMessage.style.display = '';
tooLowMessage.style.display = 'none';
tooHighMessage.style.display = 'none';
maxGuessesMessage.style.display = 'none';

submitButton.disabled = true;
guessInput.disabled = true;
Expand All @@ -42,8 +46,10 @@ function checkGuess() {
if (guess !== targetNumber) {
if (guess < targetNumber) {
tooLowMessage.style.display = '';
tooHighMessage.style.display = 'none';
} else {
tooLowMessage.style.display = '';
tooHighMessage.style.display = '';
tooLowMessage.style.display = 'none';
}

const remainingAttempts = maxNumberOfAttempts - attempts;
Expand All @@ -52,32 +58,41 @@ function checkGuess() {
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`;
}

if (attempts ==== maxNumberOfAttempts) {
if (attempts === maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
maxGuessesMessage.style.display = '';
tooHighMessage.style.display = 'none';
tooLowMessage.style.display = 'none';

}

guessInput.value = '';

resetButton.style.display = '';
resetButton.style.display= '';
}

function hideAllMessages() {
for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) {
for ( let elementIndex = 0; elementIndex < messages.length; elementIndex++) {
messages[elementIndex].style.display = 'none';


//console.log(messages[elementIndex]);
}
}

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

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


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

hideAllMessages();
Expand Down