javascript - 初学者首先尝试编码摇滚,纸..游戏。不断返回“纸质胜利”
问题描述
当我选择“摇滚”时,我不断获得“纸上奖”。
function game() {
pChoice = prompt('What is your play?');
cChoice = ['rock','paper','scissor'];
let compChoice = [Math.floor(Math.random() * cChoice.length)];
console.log(compChoice);
function compare(pChoice, compChoice) {
if (pChoice === compChoice) {
alert( "It's a tie" );
}
if (pChoice === 'rock') {
if (compChoice === 'scissor') {
return 'rock wins';
} else {
return 'paper wins';
}
}
else if (pChoice === 'paper') {
if (compChoice === 'rock') {
return 'paper wins';
} else {
return 'scissor wins';
}
}
else if (pChoice === 'scissor') {
if (compChoice === 'paper') {
return 'scissor wins';
} else {
return 'rock wins';
}
}
}
var result = compare(pChoice, compChoice);
console.log(compare(pChoice, compChoice));
}
game();
解决方案
您永远不会将 cChoice 分配给一个值,而是将其分配给一个包含数字的数组。
例如:
// Assigning to [number]
let compChoice = [Math.floor(Math.random() * cChoice.length)];
// Assigning to string inside of array cChoice
let compChoice = cChoice[Math.floor(Math.random() * cChoice.length)];
推荐阅读
- oracle - 是连续应用 oracle OBIEE 捆绑补丁还是只应用最新的
- git - git重置了,现在我无法推送
- jupyter-notebook - 如何重新排序 jupyter 笔记本中的单元格?
- android - 在使用 addTextChangeListener 和更新值时遇到问题?
- json - 调用 .NET Web API 时出现 Flurl 反序列化问题
- android - android.os.DeadSystemException 由 GooglePlayServicesUtilLight.getRemoteContext
- python - 如何断言 Pandas 数据框列中的所有值都具有所需的长度
- python - 如何从 csv 文件中删除停用词
- php - 如何在php中以间隔获取两个日期时间戳之间的日期时间范围
- python - 如何通过外键获取数据