Last active
September 29, 2021 00:07
-
-
Save amgKM/df916587f65bbd9af6c61f5339ca6de0 to your computer and use it in GitHub Desktop.
Number Guesser Codecademy
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 the new secret target number | |
const generateTarget = () => Math.floor(Math.random() * 10); | |
// Calculate the distance from the computer guess to the target and from the human guess to the target | |
const getAbsoluteDistance = (num1, num2) => Math.abs(num1 - num2); | |
const compareGuesses = (humanGuess, computerGuess, targetNumber) => { | |
alert('Set a number between 0 and 9') && (humanGuess > 9 || humanGuess < 0); // Alert the user that their number is out of range | |
const humanDiff = getAbsoluteDistance(humanGuess, targetNumber); | |
const computerDiff = getAbsoluteDistance(computerGuess, targetNumber); | |
return humanDiff <= computerDiff; | |
} | |
const updateScore = winner => winner === 'human' ? humanScore += 1 : computerScore += 1; | |
const advanceRound = () => currentRoundNumber += 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment