英文:
How can I make this loop/function run correctly?
问题
I noticed that you want the code portion to remain untranslated. The code section is enclosed in triple backticks (```), so I'll only provide the translation for the non-code part.
Here's the non-code part of your text:
"我正在制作一个使用提示来玩的剪刀石头布游戏。以下是整个代码。"
If you have any specific questions or need assistance with the code itself, please feel free to ask.
英文:
I am making a rock paper scissor game that uses prompts to play.
Here's the entire code
var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;
var winStat = 0;
var lossStat = 0;
var tieStat = 0;
function isValid() {
for (var i = 0; i < options.length; i++) {
const found = options[i];
if (userRes === found) {
return checkVar = true;
} else if (userRes !== found) {
return checkVar = false;
}
}
}
function getCompChoice() {
let compSet = options[Math.floor(Math.random() * options.length)];
compChoice = compSet.toUpperCase();
console.log(compChoice)
return alert('The computer chose ' + compChoice);
}
function getUserChoice () {
userSet = prompt('Rock Paper or Scissors?');
if (userSet === null) {
return startGame();
} else {
userRes = userSet.toUpperCase();
}
isValid()
if (checkVar === true) {
console.log('continue')
userRes.toUpperCase();
console.log(userRes);
getCompChoice()
if((userRes === 'R' && compChoice === 'S') ||
(userRes === 'P' && compChoice === 'R' ||
(userRes === 'S' && compChoice === 'P'))) {
console.log('win')
alert('You Won !')
checkStat = true
playAgain()
} else if (userRes === compChoice) {
console.log('tie')
alert('You Tied !')
checkStat = null
playAgain()
} else if (userRes !== compChoice) {
console.log('loss')
alert('You Lost !')
checkStat = false
playAgain()
}
} else if (checkVar === false) {
console.log('end')
console.log(userRes);
alert('Please enter R, P, or S. (Not case sensitive).');
getUserChoice();
}
}
function playAgain() {
if (checkStat) {
winStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (checkStat === null){
tieStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (!checkStat) {
lossStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
}
pAgain = confirm('Play Again ?')
if (pAgain) {
getUserChoice();
}
}
function startGame () {
askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
if (askUser) {
return getUserChoice();
} else if (!askUser) {
return alert('Come back next time !')
}
}
startGame();
Now my problem here is that everything works as expected however the function isValid()
only loops once and outputs only 'R' into my console log which breaks the game entirely and I can only use r to play using 'S' or 'P' returns the alert saying I did not enter r p or s if anyone can help me understand why the loop is only running once or only outputting 'R' that would be great I've been stumped on this for a while
Just FYI: I am still fairly new to JavaScript
I tried using a const to change my array to string outputs in console but that also did not do anything.
答案1
得分: 0
问题在于你提前返回了!就像这样:
function isValid() {
return checkVar = (userRes === options[0]);
}
所以解决方法是在返回之前检查所有结果:
function isValid() {
for (var i = 0; i < options.length; i++) {
const found = options[i];
if (userRes === found) {
return checkVar = true;
}
}
checkVar = false;
}
var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;
var winStat = 0;
var lossStat = 0;
var tieStat = 0;
function isValid() {
for (var i = 0; i < options.length; i++) {
const found = options[i];
if (userRes === found) {
return checkVar = true;
}
}
checkVar = false;
}
function getCompChoice() {
let compSet = options[Math.floor(Math.random() * options.length)];
compChoice = compSet.toUpperCase();
console.log(compChoice)
return alert('The computer chose ' + compChoice);
}
function getUserChoice () {
userSet = prompt('Rock Paper or Scissors?');
if (userSet === null) {
return startGame();
} else {
userRes = userSet.toUpperCase();
}
isValid()
if (checkVar === true) {
console.log('continue')
userRes.toUpperCase();
console.log(userRes);
getCompChoice()
if((userRes === 'R' && compChoice === 'S') ||
(userRes === 'P' && compChoice === 'R' ||
(userRes === 'S' && compChoice === 'P'))) {
console.log('win')
alert('You Won !')
checkStat = true
playAgain()
} else if (userRes === compChoice) {
console.log('tie')
alert('You Tied !')
checkStat = null
playAgain()
} else if (userRes !== compChoice) {
console.log('loss')
alert('You Lost !')
checkStat = false
playAgain()
}
} else if (checkVar === false) {
console.log('end')
console.log(userRes);
alert('Please enter R, P, or S. (Not case sensitive).');
getUserChoice();
}
}
function playAgain() {
if (checkStat) {
winStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (checkStat === null){
tieStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (!checkStat) {
lossStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
}
pAgain = confirm('Play Again ?')
if (pAgain) {
getUserChoice();
}
}
function startGame () {
askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
if (askUser) {
return getUserChoice();
} else if (!askUser) {
return alert('Come back next time !')
}
}
startGame();
英文:
The problem is that you are returning early!
It is like saying:
function isValid() {
return checkVar = (userRes === options[0]);
}
So the solution would be to check for all results before returning:
function isValid() {
for (var i = 0; i < options.length; i++) {
const found = options[i];
if (userRes === found) {
return checkVar = true;
}
}
checkVar = false;
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var options = ['R','P','S','r','p','s']
var userRes;
var checkVar;
var compChoice;
var checkStat;
var winStat = 0;
var lossStat = 0;
var tieStat = 0;
function isValid() {
for (var i = 0; i < options.length; i++) {
const found = options[i];
if (userRes === found) {
return checkVar = true;
}
}
checkVar = false;
}
function getCompChoice() {
let compSet = options[Math.floor(Math.random() * options.length)];
compChoice = compSet.toUpperCase();
console.log(compChoice)
return alert('The computer chose ' + compChoice);
}
function getUserChoice () {
userSet = prompt('Rock Paper or Scissors?');
if (userSet === null) {
return startGame();
} else {
userRes = userSet.toUpperCase();
}
isValid()
if (checkVar === true) {
console.log('continue')
userRes.toUpperCase();
console.log(userRes);
getCompChoice()
if((userRes === 'R' && compChoice === 'S') ||
(userRes === 'P' && compChoice === 'R' ||
(userRes === 'S' && compChoice === 'P'))) {
console.log('win')
alert('You Won !')
checkStat = true
playAgain()
} else if (userRes === compChoice) {
console.log('tie')
alert('You Tied !')
checkStat = null
playAgain()
} else if (userRes !== compChoice) {
console.log('loss')
alert('You Lost !')
checkStat = false
playAgain()
}
} else if (checkVar === false) {
console.log('end')
console.log(userRes);
alert('Please enter R, P, or S. (Not case sensitive).');
getUserChoice();
}
}
function playAgain() {
if (checkStat) {
winStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (checkStat === null){
tieStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
} else if (!checkStat) {
lossStat++;
alert('Your Stats:\nWins: ' + winStat + '\nLosses: ' + lossStat + '\nTies: ' + tieStat)
}
pAgain = confirm('Play Again ?')
if (pAgain) {
getUserChoice();
}
}
function startGame () {
askUser = confirm('Would you like to play a game of Rock, Paper, Scissors ?')
if (askUser) {
return getUserChoice();
} else if (!askUser) {
return alert('Come back next time !')
}
}
startGame();
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论