2 回答

TA贡献1812条经验 获得超5个赞
问题是你只得到一次电脑选择。您应该在每次单击时获得计算机选择。
const choices = ["rock", "paper", "scissors"];
let computersChoice ;
const rockButton = document.getElementById("rockbtn");
const updateText = document.getElementById('textField')
let yourChoice
let status
let statusComp
//function for when user clicks rock button
function userRock() {
rockButton.addEventListener ("click", function() {
yourChoice = choices[0];
computersChoice = choices[Math.floor(Math.random() * choices.length)];
execute()
});
}
function execute(){
checker()
computersTurn()
}
// checks to see if user made a choice
function checker(){
if (yourChoice === choices[0]) {
status ='rock'
}
}
// computer chooses
function computersTurn() {
statusComp = computersChoice
//logs check to make sure the program is running correctly
console.log(status)
console.log(statusComp)
if (status === statusComp) {
updateText.innerText = 'It\s a tie'
} else if (status === 'rock' && statusComp === 'paper'){
updateText.innerText = 'You lose... Computer chose paper'
} else if (status === 'rock' && statusComp === 'scissors'){
updateText.innerText = 'You win... Computer chose scissors'
}
}
function startGame(){
userRock()
}
startGame()
<button id="rockbtn">Play</button>
<span id="textField"/></span>

TA贡献1827条经验 获得超8个赞
这很简单。只需将创建计算机选项的一行移动到您的函数即可。这样,计算机就不会一直使用相同的结果。其他一切都可以保持不变,并且会起作用。computerTurns()
const choices = ["rock", "paper", "scissors"];
const rockButton = document.getElementById("rockbtn");
const updateText = document.getElementById('textField')
let computersChoice;
let yourChoice
let status
let statusComp
//function for when user clicks rock button
function userRock() {
rockButton.addEventListener ("click", function() {
yourChoice = choices[0]
execute()
});
}
function execute(){
checker()
computersTurn()
}
// checks to see if user made a choice
function checker(){
if (yourChoice === choices[0]) {
status ='rock'
}
}
// computer chooses
function computersTurn() {
computersChoice = choices[Math.floor(Math.random() * choices.length)];
statusComp = computersChoice
//logs check to make sure the program is running correctly
console.log(status)
console.log(statusComp)
if (status === statusComp) {
updateText.innerText = 'It\s a tie'
} else if (status === 'rock' && statusComp === 'paper'){
updateText.innerText = 'You lose... Computer chose paper'
} else if (status === 'rock' && statusComp === 'scissors'){
updateText.innerText = 'You win... Computer chose scissors'
}
}
function startGame(){
userRock()
}
startGame()
<button id="rockbtn">Rock</button>
<textarea id="textField"></textarea>
添加回答
举报