-
-
Save codecademydev/482e436501f6300446a33754e915611d 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
// Variables to store the human and computer scores, and the ongoing round number | |
let humanScore = 0; | |
let computerScore = 0; | |
let currentRoundNumber = 1; | |
// Function to generate the target | |
const generateTarget = () => Math.floor(Math.random() * 10); | |
// Function to compare guesses in terms of the target and return the result | |
const compareGuesses = (userGuess, computerGuess, secretTarget) => { | |
checkUserGuess(userGuess); | |
return getAbsoluteDistance(userGuess, target) <= getAbsoluteDistance(computerGuess, target) ? true : false; | |
}; | |
// Function to update the scores | |
const updateScore = (winner) => { | |
winner === 'human' ? humanScore++ : computerScore++; | |
}; | |
// Function to increment the round number | |
const advanceRound = () => { | |
currentRoundNumber ++; | |
} | |
// Function to generate the absolute distance between the guess and the target | |
const getAbsoluteDistance = (guess, target) => Math.abs(guess - target); | |
// Function to check if the User guess is within range | |
const checkUserGuess = guess => { | |
if (userGuess > 9 || userGuess < 0) { | |
alert('Guess is out of range!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment