Created
August 24, 2023 05:23
-
-
Save 87Ryu/22118d9ccce390712581ec5554772fb1 to your computer and use it in GitHub Desktop.
Number guesser
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: | |
//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