Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 6, 2022 11:44
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/b8e2a3abc750d92e5ae6ad9b1fa90df8 to your computer and use it in GitHub Desktop.
Save codecademydev/b8e2a3abc750d92e5ae6ad9b1fa90df8 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 generateTarget() {
return Math.floor(Math.random() * 10);
}
function compareGuesses(humanGuess, computerGuess, targetNum) {
//Checks that the user entered a number between 0 and 9
if (checkUserInput(humanGuess)) {
//If this is true, I would like to reset the round, but I don't know how to do that.
//So for now the program will run after the user closes the alert.
}
//Calculates the absolute distance.
let humanResult = getAbsoluteDistance(targetNum, humanGuess);
let computerResult = getAbsoluteDistance(targetNum, computerGuess);
//Compares the results to determine winner
if (humanResult < computerResult) {
return true;
}
else if (computerResult === humanResult) {
return true;
}
else {
return false;
}
}
function getAbsoluteDistance(target, guess) {
let result = target - guess;
if (result < 0) {
result *= -1;
}
return result;
}
function updateScore(winner) {
if (winner === 'human') {
humanScore++;
}
else if (winner === 'computer') {
computerScore++;
}
}
function advanceRound() {
currentRoundNumber++;
}
function checkUserInput (userInput) {
if (userInput > 9 || userInput < 0) {
window.alert("Please enter a number between 0 and 9.");
return true;
}
else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment