Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 9, 2021 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/470b9a3862370a602bdcb118f4ae295e to your computer and use it in GitHub Desktop.
Save codecademydev/470b9a3862370a602bdcb118f4ae295e to your computer and use it in GitHub Desktop.
Codecademy export
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
// Generates random 0-9 number
const generateTarget = () => Math.floor(Math.random() * 10);
// Calculates guess variances
const getAbsoluteDistance = (num1,num2) => num1 - num2;
// Compares guesses to target and issues alert for guess range
function compareGuesses(human,cpu,target){
let humanPoints = getAbsoluteDistance(target - human);
let cpuPoints = getAbsoluteDistance(target - cpu);
// Range alert
if (human <= 9) {
alert('Number is out of range. It’s not possible to set a number outside this range with the + and = buttons')
}
// returns true for human winner
if (humanPoints <= cpuPoints){
return true;
} else {
return false;
}
}
// Adds to winner's score
function updateScore(winner) {
if (winner === 'human') {
humanScore += 1;
} else {
computerScore += 1;
}
}
// Advances number of rounds
const advanceRound = () => currentRoundNumber += 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment