Skip to content

Instantly share code, notes, and snippets.

@amgKM
Last active September 29, 2021 00:07
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 amgKM/df916587f65bbd9af6c61f5339ca6de0 to your computer and use it in GitHub Desktop.
Save amgKM/df916587f65bbd9af6c61f5339ca6de0 to your computer and use it in GitHub Desktop.
Number Guesser Codecademy
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