在函数getComputerChoice-javascript程序中定义computerChoice时出错
在codeacademy javascript课程中,我正在做一个简单的石头、布、剪刀程序。第一个非常简单,这里的大部分内容都是他们指导我的方式。我知道整件事可以写得更好,但那是以后的事了。程序部分工作,但似乎从未定义computerChoice变量。我认为问题在第15-18行,但不确定。我确实尝试将第42、49和58行中的字符串更改为整数,但这并没有解决问题。 任何人都可以看看我的代码并提供指导在函数getComputerChoice-javascript程序中定义computerChoice时出错,javascript,Javascript,在codeacademy javascript课程中,我正在做一个简单的石头、布、剪刀程序。第一个非常简单,这里的大部分内容都是他们指导我的方式。我知道整件事可以写得更好,但那是以后的事了。程序部分工作,但似乎从未定义computerChoice变量。我认为问题在第15-18行,但不确定。我确实尝试将第42、49和58行中的字符串更改为整数,但这并没有解决问题。 任何人都可以看看我的代码并提供指导 //function to get the user's choice const getUser
//function to get the user's choice
const getUserChoice = userInput => {
userInput =
userInput.toLowerCase();
//if stmt to make sure input is valid
if (userInput === 'rock' || 'scissors' || 'paper') {
return userInput;
} else {
console.log('Invalid selection');
}//end else
}//end getUserChoice function
//function to get computer choice
const getComputerChoice = () => {
Math.floor(Math.random() * 3);
//switch case to verify & return result
switch (getComputerChoice) {
case 0:
return 'rock';
break;
case 1:
return 'paper';
break;
case 2:
return 'scissors';
break;
default:
console.log('Invalid');
break;
}//end switch
}//end getComputerChoice
//function to determine the winner
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The game is a tie';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') { return 'You Won!'; }
} // end userchoice is rock
if (userChoice === 'paper') {
if (computerChoice === 'scissors') {
return 'The computer won!';
} else {
return 'You won!';
}
} // end userchoice is paper
if (userChoice === 'scissors') {
if (computerChoice === 'rock') {
return 'The computer won!';
} else {
return 'You won!';
}
} //end userchoice is scissors
}//end winner function
//function to play the game
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log('You threw: ' + userChoice);
console.log('The computer threw: ' + computerChoice);
//call funtion to determine winner
console.log(determineWinner(userChoice, computerChoice));
}//end playGame
//function calls
playGame();
第18行应该是
switch (Math.floor(Math.random() * 3)) {
你传递给的任何值都是与案例值相比较的值。开关案例检查0到2之间的随机整数,因此将生成的随机数传递给开关。问题
经过更彻底的检查,有一些错误。演示1解决了这些错误中的每一个。详细信息将在演示中进行注释。还有一个问题
起初,我在没有你更新帖子的情况下开始了一个工作演示。演示2的功能也很好,如果你想考虑另一种方法来满足你的目标,则是有用的。
演示1
const getUserChoice=userInput=>{
userInput=userInput.toLowerCase;
//如果stmt需要确保输入有效
如果用户输入=='rock'| |'剪刀'| |'纸'{
返回用户输入;
}否则{
console.log“无效选择”;
}//结束其他
}; //end getUserChoice函数
//函数获取计算机选择
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在每个案例中,每次休息前都会有一次回归
return语句始终结束块,因此请中断
没有用,在这个过程中,开关也没有用。
将getComputerChoice更改为声明的函数
将切换结果保存在变量中
按预期返回值
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
功能选择{
让compPick=Math.floorMath.random*3;
console.log'compPick:'+compPick;
var-psr;
//切换案例以验证并返回结果
开关compPick{
案例0:
psr=‘岩石’;
打破
案例1:
psr=‘纸张’;
打破
案例2:
psr=‘剪刀’;
打破
违约:
console.log“无效”;
打破
}//结束开关
返回psr;
}
//函数来确定胜利者
const determineWinner=userChoice,computerChoice=>{
如果userChoice==computerChoice{
返回“比赛打成平局”;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
缺少条件语句的else部分,
每当用户扔石头,计算机扔石头时
剪刀,结果没有定义。
添加了else部分,现在所有7个条件都满足了~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
如果userChoice==='rock'{
如果computerChoice==“纸张”{
返回“你输了!”;
}否则{
回报你赢了!
}
}//最终用户选择是摇滚乐
如果userChoice==‘纸张’{
如果computerChoice===‘剪刀’{
返回“你输了!”;
}否则{
返回“你赢了!”;
}
}//最终用户选择是纸质的
如果userChoice===‘剪刀’{
如果computerChoice===‘摇滚乐’{
返回“你输了!”;
}否则{
返回“你赢了!”;
}
}//结束用户选择是剪刀
}; //最终赢家函数
//函数来玩游戏
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
添加了一个元素,以便正确完成测试
将playGame更改为声明的函数和
更改事件侦听器的回调函数
请注意,playGame正在传递一个值。此值来自
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
功能游戏玩家{
const userChoice=getUserChoicepsr;
const computerChoice=getComputerChoice;
log“您抛出:”+userChoice;
console.log“计算机抛出:”+计算机选择;
//调用函数来确定胜利者
console.logdetermineWinneruserChoice,computerChoice;
}//结束游戏
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
引用标签
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
var sel=document.getElementById'psr';
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在标记上注册更改事件
当selectpsr上发生更改事件时,playGame将被禁用
调用并将selectpsr的值传递给
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
sel.ADDEVENTLISTER'change',函数E{
playGamethis.value;
};
选择
选择权{
字体:继承
}
psr{
利润率:30px 0 0 30px
}
--
纸张
剪刀
岩石
请把你的要点写在你的帖子里,谢谢。我很抱歉,我不知道这到底是什么意思。。创建要点,然后将整个程序剪切粘贴到消息中?我们永远不需要整个程序,除非它实际上只是几行或几行代码。这里真正应该介绍的是一个针对具体问题的解决方案。我以为我已经准备好了 选择的值等于Math.random字符串的值。。。再次感谢