Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 8, 2020 20:18
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/5682a6ed7293dc901354ee1e35d8d633 to your computer and use it in GitHub Desktop.
Save codecademydev/5682a6ed7293dc901354ee1e35d8d633 to your computer and use it in GitHub Desktop.
Codecademy export
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
// function for generate a random # for the game
let generateTarget = () => {
return Math.floor(Math.random()*10);
};
//function for calculate the absolute distance
let getAbsoluteDistance = (guessNum, targetNum) => {
return Math.abs(targetNum - guessNum);
};
//funstion to determine which guess is closet to the target #
let compareGuesses = (human, computer, target) => {
// const humanNum = Math.abs(target - human);
// const computerNum = Math.abs(target - computer);
const humanNum = getAbsoluteDistance(human, target);
const computerNum = getAbsoluteDistance(computer, target);
//function for limiting the user guess # between 0-9
if (human > 9 || human < 0) {
window.alert('The number you have entered is out of range. Please enter a number between 0 to 9');
return false;
}else if (humanNum <= computerNum) {
return true;
}else {
return false;
};
};
//function for updating the score
const updateScore = (winner) => {
if (winner === 'human') {
humanScore ++;
}else if (winner === 'computer'){
computerScore ++;
};
};
//function for updating # of round
const advanceRound = () => {
return currentRoundNumber ++;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment