如何使这个循环/函数运行正确?

huangapple go评论65阅读模式
英文:

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 &lt; 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 = [&#39;R&#39;,&#39;P&#39;,&#39;S&#39;,&#39;r&#39;,&#39;p&#39;,&#39;s&#39;]
var userRes;
var checkVar;
var compChoice;
var checkStat;
var winStat = 0;
var lossStat = 0;
var tieStat = 0;
function isValid() {
for (var i = 0; i &lt; 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(&#39;The computer chose &#39; + compChoice);
}
function getUserChoice () {
userSet = prompt(&#39;Rock Paper or Scissors?&#39;);
if (userSet === null) {
return startGame();
} else {
userRes = userSet.toUpperCase();
}
isValid()
if (checkVar === true) {
console.log(&#39;continue&#39;)
userRes.toUpperCase();
console.log(userRes);
getCompChoice()
if((userRes === &#39;R&#39; &amp;&amp; compChoice === &#39;S&#39;) ||
(userRes === &#39;P&#39; &amp;&amp; compChoice === &#39;R&#39; ||
(userRes === &#39;S&#39; &amp;&amp; compChoice === &#39;P&#39;))) {
console.log(&#39;win&#39;)
alert(&#39;You Won !&#39;)
checkStat = true
playAgain()
} else if (userRes === compChoice) {
console.log(&#39;tie&#39;)
alert(&#39;You Tied !&#39;)
checkStat = null
playAgain()
} else if (userRes !== compChoice) {
console.log(&#39;loss&#39;)
alert(&#39;You Lost !&#39;)
checkStat = false
playAgain()
}
} else if (checkVar === false) {
console.log(&#39;end&#39;)
console.log(userRes);
alert(&#39;Please enter R, P, or S. (Not case sensitive).&#39;);
getUserChoice();
}
}
function playAgain() {
if (checkStat) {
winStat++;
alert(&#39;Your Stats:\nWins: &#39; + winStat + &#39;\nLosses: &#39; + lossStat + &#39;\nTies: &#39; + tieStat)
} else if (checkStat === null){
tieStat++;
alert(&#39;Your Stats:\nWins: &#39; + winStat + &#39;\nLosses: &#39; + lossStat + &#39;\nTies: &#39; + tieStat)
} else if (!checkStat) {
lossStat++;
alert(&#39;Your Stats:\nWins: &#39; + winStat + &#39;\nLosses: &#39; + lossStat + &#39;\nTies: &#39; + tieStat)
}
pAgain = confirm(&#39;Play Again ?&#39;)
if (pAgain) {
getUserChoice();
}
}
function startGame () {
askUser = confirm(&#39;Would you like to play a game of Rock, Paper, Scissors ?&#39;)
if (askUser) {
return getUserChoice();
} else if (!askUser) {
return alert(&#39;Come back next time !&#39;)
}
}
startGame();

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 07:06:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定