如果HTML中的span=0并且函数返回真值,我希望内部HTML值增加1。目前,它只是像火车车一样添加数字1和0(HTML=0,函数运行,HTML=01…010101010,而不是HTML=0,函数,HTML=1),它对我的松散和获胜的HTML span Id都这样做。
我尝试了各种各样的想法,但最终得到了更不理想的结果。
这是我的第一个java脚本项目。我知道这里很乱。我该如何改进?
<html>
<div class="computerChoice">Radi-claws choice: <span id="computer-choice"></span></div>
<div class="killerKittenChoice">Killer-Kittens choice: <span id="user-choice"></span></div>
<div class="imediateResult">Results: <span id="result"></span></div>
<div class="over-all-results" >Best out of 5:<br> Killer kitten: <span id="user-history">0</span> <br>
Radi-Claws: <span id="pc-history">0</span></div> </h2>
<script type="text/javascript" src="script.js"></script>
</hmtl>
<script>
const pcWeaponDisplay = document.getElementById("computer-choice")
const userChoiceDisplay = document.getElementById("user-choice")
const resultDisplay = document.getElementById("result")
const possibleChoices = document.querySelectorAll("button")
const userWinCountDisplay = document.getElementById("user-history")
const pcWinCountDisplay = document.getElementById("pc-history")
let userChoice
let computerChoice
let finalResults
let plusWin
// click of button results in the funtions to be run
possibleChoices.forEach(possibleChoice => possibleChoice.addEventListener("click", (e) => {
userChoice = e.target.id
userChoiceDisplay.innerHTML = userChoice
pcWeaponChoice()
results()
whoWon()
pcWhoWon()
}))
// pc random choice
function pcWeaponChoice() {
pcWeapon = [0, 1, 2];
const random = Math.floor(Math.random() * pcWeapon.length);
console.log (random, pcWeapon[random]);
if (random === 0) {
computerChoice = "Spray Bottle"
}
if (random === 1) {
computerChoice = "Cucumber"
}
if (random === 2) {
computerChoice = "Box"
}
pcWeaponDisplay.innerHTML = computerChoice
}
// results - function compares pcWeapon and userchoice
function results () {
// draw options
if (computerChoice == "Spray Bottle" && userChoice == "Spray Bottle") {
finalResults = "Draw! You are both wet pussies!"
}
if (computerChoice == "Cucumber" && userChoice == "Cucumber") {
finalResults = "Draw! You both ran away from a snake!"
}
if (computerChoice == "Box" && userChoice == "Box") {
finalResults = "Draw! A minute has passed and you both are still turtles."
}
//win options
if (computerChoice == "Spray Bottle" && userChoice == "Cucumber") {
finalResults = "Win! You wacked that puss!"
}
if (computerChoice == "Box" && userChoice == "Spray Bottle") {
finalResults = "Win! Radi-claws is a wet puss!"
}
if (computerChoice == "Cucumber" && userChoice == "Box") {
finalResults = "Win! Your box blocked the penetration!"
}
// Lose
if (computerChoice == "Spray Bottle" && userChoice == "Box") {
finalResults = "Looooooohh-Zzzeerrrr! You got soaked!"
}
if (computerChoice == "Cucumber" && userChoice == "Spray Bottle") {
finalResults = "Boo Booo you baby! Be better, you thought he had a snake!"
}
if (computerChoice == "Box" && userChoice == "Cucumber") {
finalResults = "Weak throw! You couldn't penetrate Radi-claws!"
}
resultDisplay.innerHTML = finalResults
}
// function incrementScore(scoreSpan)
// scoreSpan.innerText = parseInt(scoreSpan.innerText) + 1;
// increment winner display +1
function whoWon() {
if (resultDisplay == "Win! You wacked that puss!", "Win! Radi-claws is a wet puss!", "Win! Your box blocked the penetration!") {
userWinCountDisplay.innerText = (userWinCountDisplay.innerText) + 1
} else (resultDisplay != "Win! You wacked that puss!", "Win! Radi-claws is a wet puss!", "Win! Your box blocked the penetration!")
userWinCountDisplay.innerText = (userWinCountDisplay.innerText) + 0
}
</script>