Last active
February 28, 2021 14:35
-
-
Save izzy-baulk/c69d89d5d7fcbb0255840222c483a9a6 to your computer and use it in GitHub Desktop.
CodeCademy Number Guesser Challenge Project (JavaScript)
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; | |
const generateTarget = () => Math.floor(Math.random() * 9); | |
const getAbsoluteDistance = (guess, targetGuess) => Math.abs(guess-targetGuess) | |
function compareGuesses(userGuess, computerGuess, targetGuess) { | |
if (userGuess > 9 || userGuess < 0) { | |
alert('guess out of range') | |
} else { | |
let humanDifference = getAbsoluteDistance(userGuess, targetGuess) | |
let computerDifference = getAbsoluteDistance(computerGuess,targetGuess) | |
if (humanDifference === computerDifference) { | |
return true; | |
} else if (humanDifference < computerDifference) { | |
return true; | |
} else if (humanDifference > computerDifference) { | |
return false; | |
} | |
} | |
} | |
function updateScore (winner) { | |
winner === 'human' ? humanScore ++ : computerScore ++; | |
} | |
const advanceRound = () => currentRoundNumber ++; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment