Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 24, 2020 09:13
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/482e436501f6300446a33754e915611d to your computer and use it in GitHub Desktop.
Save codecademydev/482e436501f6300446a33754e915611d to your computer and use it in GitHub Desktop.
Codecademy export
// 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