-
-
Save codecademydev/5682a6ed7293dc901354ee1e35d8d633 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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