我在做這個石頭、紙和剪刀的游戲,但是,我的最后一個函式不能正常作業,它不會執行那個代碼塊,最后一個函式中的變數有什么問題,它們不能被呼叫,我試圖更改引數,我還除錯了代碼,并且在某種程度上我注意到它只是最后一個函式不能正常作業
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
return Math.floor(Math.random() * any)
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' userChoice);
console.log('The computer threw: ' computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
uj5u.com熱心網友回復:
如發布的那樣,getComputerChoice只是進行亂數學運算并乘以(有時缺少)引數。['rock', 'paper', 'scissors']考慮更改以從...的集合中回傳一個隨機選擇
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice() {
const choices = ['rock', 'paper', 'scissors'];
return choices[Math.floor(Math.random() * choices.length)]
}
switch (getComputerChoice(3)) //userInput
{
case 0:
console.log('rock');
break;
case 1:
console.log('paper');
break;
case 2:
console.log('scissors');
break;
default:
console.log('Select a valid option')
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You won'
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'Computer won!'
} else {
return 'You won!'
}
}
}
const playGam = () => {
const userChoice = getUserChoice('scissors');
const computerChoice = getComputerChoice();
console.log('You threw: ' userChoice);
console.log('The computer threw: ' computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
我們還可以通過其他一些方式改進代碼......
// make this global
const choices = ['rock', 'paper', 'scissors'];
const beats = { rock: 'paper', paper: 'scissors', scissors: 'rock' };
// general purpose fn to validate
const validateChoice = choice => {
return choices.includes(choice.toLowerCase());
};
// a tightly specified thing to determine winner
const determineWinner(userChoice, computerChoice) {
if (computerChoice === userChoice) return 'tie';
return beats[userChoice] === computerChoice ? 'user win' : 'computer win';
}
uj5u.com熱心網友回復:
您沒有使用 swtich 陳述句設定變數值。你需要回傳計算機的選擇,所以我把你的 switch 陳述句放在getComputerChoice函式中,然后它回傳輸出,你可以在你的playGam函式中使用它。我也更新了它以在每個結果中顯示獲勝者。
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput == "rock" || userInput == "paper" || userInput == "scissors") {
return userInput;
} else {
console.log('Enter a valid option')
};
};
function getComputerChoice(any) {
const randomNo = Math.floor(Math.random() * any)
switch (randomNo) //userInput
{
case 0:
return 'scissors';
break;
case 1:
return 'paper';
break;
case 2:
return 'rock';
break;
default:
return 'Select a valid option';
}
}
function determineWinner(getUserChoice, getComputerChoice) {
if (getUserChoice === getComputerChoice) {
return 'The game was a tie'
}
switch (getUserChoice) {
case 'paper':
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'rock' :
return getComputerChoice === 'scissors' ? 'The computer won!' : 'You won';
case 'scissors' :
return getComputerChoice === 'paper' ? 'You won' : 'The computer won!';
default:
return 'Something went wrong'
}
}
const playGam = () => {
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice(3);
console.log('You threw: ' userChoice);
console.log('The computer threw: ' computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
playGam();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486825.html
標籤:javascript
上一篇:如何清除反應div中的所有輸入
