-
-
Save codecademydev/ca93fbc3bca9727622ed6271ea3fde2c to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let target; | |
const humanGuessInput = document.getElementById('human-guess'); | |
const roundNumberDisplay = document.getElementById('round-number'); | |
const computerGuessDisplay = document.getElementById('computer-guess'); | |
const humanScoreDisplay = document.getElementById('human-score'); | |
const computerScoreDisplay = document.getElementById('computer-score'); | |
const targetNumberDisplay = document.getElementById('target-number'); | |
const computerWinsDisplay = document.getElementById('computer-wins'); | |
const guessButton = document.getElementById('guess'); | |
const nextRoundButton = document.getElementById('next-round'); | |
guessButton.addEventListener('click', () => { | |
// Generate the target value | |
target = generateTarget(); | |
// Retrieve the player's guess | |
const currentHumanGuess = humanGuessInput.value; | |
// Make a random 'computer guess' | |
const computerGuess = Math.round(Math.random() * 10); | |
// Display the computer guess and the target | |
computerGuessDisplay.innerText = computerGuess; | |
targetNumberDisplay.innerText = target; | |
// Determine if the human or computer wins: | |
const winner = compareGuesses(currentHumanGuess, computerGuess, target); | |
// Update the correct score: | |
updateScore(winner); | |
// Display the winner | |
if (winner == 'human') { | |
guessButton.innerText = 'You Win!!!!!'; | |
guessButton.classList.toggle('winning-text'); | |
} else { | |
computerWinsDisplay.innerText = 'Computer Wins!!!'; | |
} | |
// winnerDisplay.innerText = humanIsWinner ? 'You win!' : 'Computer wins!'; | |
// Display the current scores: | |
humanScoreDisplay.innerText = humanScore; | |
computerScoreDisplay.innerText = computerScore; | |
// Set the correct disabled state for the buttons | |
guessButton.setAttribute('disabled', true); | |
nextRoundButton.removeAttribute('disabled'); | |
handleValueChange(humanGuessInput.value); | |
}); | |
nextRoundButton.addEventListener('click', () => { | |
// Increase the round number | |
advanceRound(); | |
// Display the new round number | |
roundNumberDisplay.innerText = currentRoundNumber; | |
// Set the correct disabled state for the buttons | |
nextRoundButton.setAttribute('disabled', true); | |
guessButton.removeAttribute('disabled'); | |
// Reset the guess input box and the target number display: | |
targetNumberDisplay.innerText = '?'; | |
guessButton.innerText = 'Make a Guess'; | |
computerGuessDisplay.innerText = '?'; | |
computerWinsDisplay.innerText = ''; | |
guessButton.classList.remove('winning-text'); | |
handleValueChange(humanGuessInput.value); | |
}); | |
const addButton = document.getElementById('add'); | |
const subtractButton = document.getElementById('subtract'); | |
addButton.addEventListener('click', () => { | |
humanGuessInput.value = +humanGuessInput.value + 1; | |
handleValueChange(humanGuessInput.value); | |
guessButton.innerText = 'Make a Guess'; | |
guessButton.classList.remove('winning-text'); | |
}); | |
subtractButton.addEventListener('click', () => { | |
humanGuessInput.value = +humanGuessInput.value - 1; | |
handleValueChange(humanGuessInput.value); | |
guessButton.innerText = 'Make a Guess'; | |
guessButton.classList.remove('winning-text'); | |
}); | |
const handleValueChange = value => { | |
if (value) { | |
if (nextRoundButton.hasAttribute('disabled')) { | |
if (value >= 0 && value <= 10) { | |
guessButton.removeAttribute('disabled'); | |
guessButton.classList.remove('winning-text'); | |
if (value > 0 && value < 10) { | |
addButton.removeAttribute('disabled'); | |
subtractButton.removeAttribute('disabled'); | |
} | |
if (value == 0 || value == 10) { | |
if (value == 0) { | |
subtractButton.setAttribute('disabled', true); | |
addButton.removeAttribute('disabled'); | |
} | |
if (value == 10) { | |
addButton.setAttribute('disabled', true); | |
subtractButton.removeAttribute('disabled'); | |
} | |
} | |
} else { | |
guessButton.setAttribute('disabled', true); | |
if (value < 0) { | |
subtractButton.setAttribute('disabled', true); | |
addButton.removeAttribute('disabled'); | |
} | |
if (value > 10) { | |
addButton.setAttribute('disabled', true); | |
subtractButton.removeAttribute('disabled'); | |
} | |
} | |
} else { | |
addButton.setAttribute('disabled', true); | |
subtractButton.setAttribute('disabled', true); | |
guessButton.setAttribute('disabled', true); | |
} | |
} else { | |
guessButton.setAttribute('disabled', true); | |
} | |
} | |
humanGuessInput.addEventListener('input', function(e) { | |
handleValueChange(e.target.value); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let humanScore = 0; | |
let computerScore = 0; | |
let currentRoundNumber = 1; | |
// Generate Target Function | |
function generateTarget() { | |
return Math.round(Math.random() * 10); | |
} | |
// Compare Guesses Function | |
function compareGuesses(HUMg, COMg, tar) { | |
let closestGuess; | |
let humanGuess = (h,t) => Math.abs(t-h); | |
let computerGuess = (c,t) => Math.abs(t-c); | |
closestGuess = ( (humanGuess(HUMg, tar) < computerGuess(COMg, tar)) || (humanGuess(HUMg, tar) == computerGuess(COMg, tar)) ) ? | |
'human' : 'computer'; | |
console.log(closestGuess); | |
return closestGuess; | |
} | |
// Update Score Function | |
function updateScore(victor){ | |
if (victor == 'human'){ | |
humanScore++; | |
} else {computerScore++;} | |
} | |
// Advance Round Function | |
function advanceRound(){ | |
currentRoundNumber++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Number Guesser Gist