Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 11, 2020 13:26
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/7a7c5e455bddc225b4c5442acd9fbe3b to your computer and use it in GitHub Desktop.
Save codecademydev/7a7c5e455bddc225b4c5442acd9fbe3b 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 to generate a random target number
// between 0 and 9
function generateTarget(){
return Math.floor((Math.random() * 8) + 1);
}
//function to get the distance from target to
//the guessed number
const getAbsoluteDistance = (guessedNum, Target) => Math.abs(Target - guessedNum);
// fucntion to compare guess of user and computer
// and return true/false accordingly
function compareGuesses(userGuess, compGuess, targetNum){
if(userGuess > 0 && userGuess < 9)
{
const userGuessDifference = getAbsoluteDistance(userGuess, targetNum);
const compGuessDifference = getAbsoluteDistance(compGuess, targetNum);
if(userGuessDifference < compGuessDifference)
{
return true;
}
else if(userGuessDifference > compGuessDifference)
{
return false;
}
else
{
return true;
}
}
else{
alert('The number should be between 0 and 9!');
/* setting again the currentRoundNumber
to the round in which user input was out
of range */
currentRoundNumber -= 1;
}
}
// function to the update the score of winner
function updateScore(winner){
if(winner === 'human')
{
humanScore += 1;
}
else
{
computerScore += 1;
}
}
// function to advance the round number
function advanceRound(){
currentRoundNumber += 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment