【发布时间】:2020-05-02 02:14:19
【问题描述】:
为什么我的变量 pc_score 和 my_score 没有增加?输出为 0。 我从代码中省略了另外两个 eventListener 格式的函数,但它不应该影响结果。 . 我编辑了可运行代码的帖子。谢谢你。 感谢您查看我的问题。
<script>
var options = ['rock','paper','scissors']
let my_score = 0;
var pc_score = 0;
let computerChoice ;
function computerSelection() {
computerChoice = options[Math.floor(Math.random()*options.length)]
return computerChoice;
}
var results = document.getElementById("result")
document.getElementById("rock").addEventListener("click", ()=> {
playerChoice=rock;
computerSelection();
if (computerChoice=='rock'){
results.innerHTML="It is a tie";
} else if (computerChoice=='paper'){
results.innerHTML="It is a loss";
pc_score += 1;
}else if (computerChoice=='scissors'){
results.innerHTML="It is a win";
my_score+= 1
}
})
const you = document.getElementById("You")
you.innerHTML= my_score
const computer = document.getElementById("Computer")
computer.innerHTML=pc_score
</script>
【问题讨论】:
-
computerChoice未定义。请提供一个可运行的示例。 -
you.innerHTML= my_score不会绑定 my_score 到您的 HTML。它写一次,就是这样。然后增加 Javascript 变量,但不要相应地更新 HTML,因此更改不可见。 -
@JeremyThille 我该怎么做才能解决这个问题?
-
每次
my_score更改时重复you.innerHTML= my_score。
标签: javascript loops counter increment