Skip to content

Instantly share code, notes, and snippets.

@87Ryu
Created August 24, 2023 05:23
Show Gist options
  • Save 87Ryu/22118d9ccce390712581ec5554772fb1 to your computer and use it in GitHub Desktop.
Save 87Ryu/22118d9ccce390712581ec5554772fb1 to your computer and use it in GitHub Desktop.
Number guesser
let humanScore = 0;
let computerScore = 0;
let currentRoundNumber = 1;
// Write your code below:
//1st step
function generateTarget() {
return Math.floor(Math.random() * 10);
}
console.log(generateTarget());
//2nd step
const compareGuesses = (humanGuess, computerGuess, targetNumber) => {
const humanDifference = Math.abs(humanGuess - targetNumber);
const computerDifference = Math.abs(computerGuess - targetNumber);
// If humanDifference is less than or equal to computerDifference, human wins
return humanDifference <= computerDifference;
};
console.log(compareGuesses(7, 4, 3));
console.log(compareGuesses(5, 2, 8));
//3rd step
const updateScore = (winner) => {
if (winner === "human") {
humanScore += 1;
} else if (winner === "computer") {
computerScore += 1;
}
};
updateScore("computer");
console.log("Human Score:", humanScore);
console.log("Computer Score:", computerScore);
//4th step
function advanceRound() {
currentRoundNumber++;
}
console.log("Current Round:", currentRoundNumber);
advanceRound();
console.log("Current Round:", currentRoundNumber);
advanceRound();
console.log("Current Round:", currentRoundNumber);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment