-
-
Save codecademydev/19b2ebd19701089d7f2f914379433c9b 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
/*Declaration of global variables*/ | |
//------------------------------------------------------------------------------ | |
let humanScore = 0; | |
let computerScore = 0; | |
let currentRoundNumber = 1; | |
//------------------------------------------------------------------------------ | |
/*This function is used to generate the new secret target number.*/ | |
const generateTarget = () => { | |
return Math.floor(Math.random() * 10); | |
} | |
//------------------------------------------------------------------------------ | |
/*This function is used to Determine the closeness of guessed number from | |
the secret number.*/ | |
const getAbsoluteDistance = (guess, secretNumber) => { | |
return Math.abs(secretNumber - guess); | |
} | |
//------------------------------------------------------------------------------ | |
/*This function is used to determine who guesses the number close to the secret | |
number and returns results in boolean operator.*/ | |
const compareGuesses = (humanGuess, computerGuess, secretTarget) => { | |
if (humanGuess>=0 &&humanGuess<=9) { | |
let humDistance = getAbsoluteDistance(humanGuess, secretTarget); | |
let compDistance = getAbsoluteDistance(computerGuess, secretTarget); | |
if (humDistance > compDistance) { | |
return false; | |
} else if (humDistance === compDistance) { | |
return true; | |
} else { | |
return true; | |
} | |
} else { | |
window.alert(`Your input as ${humanGuess} is out of range.`) | |
} | |
} | |
//------------------------------------------------------------------------------ | |
/*This function is used for updating the score.*/ | |
const updateScore = winner => { | |
if (winner === 'human') { | |
humanScore += 1; | |
} else { | |
computerScore += 1; | |
} | |
} | |
//------------------------------------------------------------------------------ | |
/*This function is used to increase the round number.*/ | |
const advanceRound = () => { | |
currentRoundNumber += 1; | |
} | |
//------------------------------------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment