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>
添加回答
舉報