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

lesson 5.1 updated #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ main {

#reset {
margin: 20px;
display: none;
}

#message {
display: none;
}
62 changes: 33 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ const tooLowMessage = document.getElementById('too-low');
const maxGuessesMessage = document.getElementById('max-guesses');
const numberOfGuessesMessage = document.getElementById('num-of-guesses');
const correctMessage = document.getElementById('correct');
const setup = document.getElementById('setup');


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,6 +24,12 @@ function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

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

function checkGuess() {
// Get value from guess input element
const guess = parseInt(guessInput.value, 10);
Expand All @@ -37,54 +45,50 @@ function checkGuess() {

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

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

const remainingAttempts = maxNumberOfAttempts - attempts;


console.log(typeof maxNumberOfAttempts, typeof attempts, typeof guess);

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

if (attempts === maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
} else {
numberOfGuessesMessage.style.display = '';
aloroduns marked this conversation as resolved.
Show resolved Hide resolved
numberOfGuessesMessage.innerHTML = `You guessed ${guess}. <br> ${remainingAttempts} guesses remaining`;
}

if (attempts ==== maxNumberOfAttempts) {
submitButton.disabled = true;
guessInput.disabled = true;
}

guessInput.value = '';

resetButton.style.display = '';
}

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

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

funtion setup() {
function gameSetup() {
// Get random number
targetNumber = getRandomNumber(1, 100);
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();
resetButton.style.display = 'none';
}

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

aloroduns marked this conversation as resolved.
Show resolved Hide resolved
setup();
window.onload = gameSetup();