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

debugging #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
62 changes: 35 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const guessInput = document.getElementById('guess');
const submitButton = document.getElementById('submit');
const resetButton = document.getElementById('reset');
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('number-of-guesses');
const correctMessage = document.getElementById('correct');
const guessInput = document.getElementById("guess");
const submitButton = document.getElementById("submit");
const resetButton = document.getElementById("reset");
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("number-of-guesses");
const correctMessage = document.getElementById("correct");
// use of double quotes/strings corrected

let targetNumber;
let attempts = 0;
Expand All @@ -30,61 +31,68 @@ function checkGuess() {
hideAllMessages();

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

correctMessage.style.display = '';
correctMessage.style.display = "";

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

if (guess !== targetNumber) {
if (guess < targetNumber) {
tooLowMessage.style.display = '';
tooLowMessage.style.display = "";
} else {
tooLowMessage.style.display = '';
tooHighMessage.style.display = "";
}
// logic corrected to include too high as well as too low messages

const remainingAttempts = maxNumberOfAttempts - attempts;

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

if (attempts ==== maxNumberOfAttempts) {
if (attempts === maxNumberOfAttempts) {
// removed extra equal sign
submitButton.disabled = true;
guessInput.disabled = true;
}

guessInput.value = '';
guessInput.value = "";

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

function hideAllMessages() {
for (let elementIndex = 0; elementIndex <= messages.length; elementIndex++) {
messages[elementIndex].style.display = 'none';
for (let elementIndex = 0; elementIndex < messages.length; elementIndex++) {
// removed equal sign to be only less than for approximation to loop better
messages[elementIndex].style.display = "none";
}
}

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

// Reset number of attempts
maxNumberOfAttempts = 0;
// maxNumberOfAttempts = 0;
attempts = 0;
// attempts = 0; // this should just reset attempts, not max attempts, which was previously set

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

hideAllMessages();
resetButton.style.display = 'none';
resetButton.style.display = "none";
}

submitButton.addEventListener('click', checkGuess);
resetButton.addEventListener('click', setup);
submitButton.addEventListener("click", checkGuess);
resetButton.addEventListener("click", setup);

setup();
setup();