Javascript 有人能给我解释一下这段包含if-else语句的代码吗?
我正在重新学习if/else语句的基础知识,因为它们是我在学校的弱点 我正在参加一个关于代码学院的java脚本课程,我正处于if语句的阶段。我感到困惑的例子是:Javascript 有人能给我解释一下这段包含if-else语句的代码吗?,javascript,Javascript,我正在重新学习if/else语句的基础知识,因为它们是我在学校的弱点 我正在参加一个关于代码学院的java脚本课程,我正处于if语句的阶段。我感到困惑的例子是: if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'The computer won!'; } else { return 'You won!'; } } else语句是否认为userChoice等于“剪刀” 还是将用
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
else语句是否认为userChoice等于“剪刀”
还是将用户选择切换为“纸质”,将计算机选择切换为“摇滚”
我知道这是很基本的东西,但它总是让我困惑
整个功能如下所示
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'It is a tie!';
}
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
if (userChoice === 'paper') {
if (computerChoice === "scissors") {
return 'The computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'scissors') {
if (computerChoice === "rock") {
return 'The computer won!';
} else {
return 'you won!';
}
}
if (userChoice === 'bomb') {
return 'Bomb beats all';
}
}
这就是if语句的基本功能:
if (computerChoice === 'paper') {
return 'The computer won!';
} if (computerChoice !== 'paper') {
return 'You won!';
}
如果if语句为false,则调用else语句。因此,如果computerChoice不是完全纸质的,则调用else语句。标准if else将始终输出,因为只有两条路径-相等或不相等。没有中间环节。这就是if语句的基本功能:
if (computerChoice === 'paper') {
return 'The computer won!';
} if (computerChoice !== 'paper') {
return 'You won!';
}
如果if语句为false,则调用else语句。因此,如果computerChoice不是完全纸质的,则调用else语句。标准if else将始终输出,因为只有两条路径-相等或不相等。没有中间的东西。以下是解释: 注意:==检查值及其类型
if (userChoice === 'rock') { // if **userChoice** is of string **rock** that means the if condition is true and it will continue execiting the code inside if
if (computerChoice === 'paper') { // if **computerChoice** is of string **paper** it executes code inside and returns 'the computer has won '
return 'The computer won!';
} else { // if **computerChoice** was not of **string** paper it executes 'You won' because the computer chose scissors
return 'You won!';
}
}
如果userChoice与computerChoice相同,则执行此代码
if(userChoice === computerChoice){
return 'It is a tie!';
}
以下是解释: 注意:==检查值及其类型
if (userChoice === 'rock') { // if **userChoice** is of string **rock** that means the if condition is true and it will continue execiting the code inside if
if (computerChoice === 'paper') { // if **computerChoice** is of string **paper** it executes code inside and returns 'the computer has won '
return 'The computer won!';
} else { // if **computerChoice** was not of **string** paper it executes 'You won' because the computer chose scissors
return 'You won!';
}
}
如果userChoice与computerChoice相同,则执行此代码
if(userChoice === computerChoice){
return 'It is a tie!';
}
在本例中,通常首先检查userChoice值,假设它不等于computerChoice-在本例中,我们检查这两个值并相互比较
因此,在匹配第一个if块之后,将验证第二个匹配,即查找computerChoice的值
让我们假设这个场景:userChoice='scissors'
我们需要检查计算机选择的价值。如果computerChoice的价值不是“剪刀”,那么我们知道它不会是平局。我们得再找一场比赛
我们找到了与第一个条件匹配的块:
if (userChoice === 'scissors'){}
现在我们需要看看if语句中的内容,我们有:
if (computerChoice === "rock"){
return 'The computer won!';
} else {
return 'you won!';
}
这意味着如果computerChoice='rock',那么它将返回'TheComputerWind!'
如果computerChoice不是“rock”,则会运行else语句,返回“you won!”
重要注意事项:代码从上到下执行,很少执行,因此首先检查此块:
if(userChoice === computerChoice){
return 'It is a tie!';
}
如果将此块移动到函数的底部,可能会看到意外的结果
const determineWinner=userChoice,computerChoice=>{
如果userChoice==='rock'{
如果computerChoice==“纸张”{
返回“计算机赢了!”;
}
否则{
返回“你赢了!”;
}
}
如果userChoice==‘纸张’{
如果计算机选择===剪刀{
返回“计算机赢了!”;
}否则{
返回“你赢了!”;
}
}
//找到了匹配项!!!
如果userChoice===‘剪刀’{
如果计算机选择===摇滚乐{
返回“计算机赢了!”;
}否则{
//运行else语句
返回“你赢了!”;
}
}
//它没有达到这一点,而是在以下时间之前返回:
ifuserChoice==computerChoice{
return“这是平局!”;//应该是平局
}
如果userChoice==='bomb'{
返回“炸弹胜过一切”;
}
}
logdetermineWinner'scissors',scissors'在本例中,通常首先检查userChoice值,假设它不等于computerChoice-在本例中,我们检查两个值并相互比较
因此,在匹配第一个if块之后,将验证第二个匹配,即查找computerChoice的值
让我们假设这个场景:userChoice='scissors'
我们需要检查计算机选择的价值。如果computerChoice的价值不是“剪刀”,那么我们知道它不会是平局。我们得再找一场比赛
我们找到了与第一个条件匹配的块:
if (userChoice === 'scissors'){}
现在我们需要看看if语句中的内容,我们有:
if (computerChoice === "rock"){
return 'The computer won!';
} else {
return 'you won!';
}
这意味着如果computerChoice='rock',那么它将返回'TheComputerWind!'
如果computerChoice不是“rock”,则会运行else语句,返回“you won!”
重要注意事项:代码从上到下执行,很少执行,因此首先检查此块:
if(userChoice === computerChoice){
return 'It is a tie!';
}
如果将此块移动到函数的底部,可能会看到意外的结果
const determineWinner=userChoice,computerChoice=>{
如果userChoice==='rock'{
如果computerChoice==“纸张”{
返回“计算机赢了!”;
}
否则{
返回“你赢了!”;
}
}
如果userChoice==‘纸张’{
如果计算机选择===剪刀{
返回“计算机赢了!”;
}否则{
返回“你赢了!”;
}
}
//找到了匹配项!!!
如果userChoice===‘剪刀’{
如果计算机选择===摇滚乐{
返回“计算机赢了!”;
}否则{
//运行其他sta
泰门特
返回“你赢了!”;
}
}
//它没有达到这一点,而是在以下时间之前返回:
ifuserChoice==computerChoice{
return“这是平局!”;//应该是平局
}
如果userChoice==='bomb'{
返回“炸弹胜过一切”;
}
}
console.logdetermineWinner'scissors','scissors'不,不是,这是第四个IF语句处理的。
其结构化方式意味着每个IF块只处理两个输入。如果用户放入剪刀,那么它将跳过If block不是,这是第四条If语句处理的。
其结构化方式意味着每个IF块只处理两个输入。如果用户放入剪刀,那么它将跳过“如果块”,我将尝试以最好的方式解释它。对于这段代码:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
计算机按语句的顺序运行。首先,他检查用户在这个片段中选择了什么,哪个是摇滚乐。
如果userChoice==='rock'
然后,因为用户确实选择了rock,它将转到下一个语句,该语句检查计算机的选择。如果计算机选择了正确的岩石计数器,它将移动到返回的语句,即计算机选择了计数器并获胜
if (computerChoice === 'paper') {
return 'The computer won!';
但是,如果计算机选择了纸张以外的任何内容,则跳过上面的if语句,并执行else语句,这是玩家赢得的
else {
return 'You won!';
}
我会尽力用最好的方式来解释。对于这段代码:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
计算机按语句的顺序运行。首先,他检查用户在这个片段中选择了什么,哪个是摇滚乐。
如果userChoice==='rock'
然后,因为用户确实选择了rock,它将转到下一个语句,该语句检查计算机的选择。如果计算机选择了正确的岩石计数器,它将移动到返回的语句,即计算机选择了计数器并获胜
if (computerChoice === 'paper') {
return 'The computer won!';
但是,如果计算机选择了纸张以外的任何内容,则跳过上面的if语句,并执行else语句,这是玩家赢得的
else {
return 'You won!';
}
else语句是否认为userChoice等于“剪刀”?。是的。流程非常简单:由于首先测试了代码的平等性,userChoice并不是纸上谈兵。因此,在else中,由于userChoice不是石头,所以它只能是剪刀。else语句是否认为userChoice等于剪刀?。是的。流程非常简单:由于首先测试了代码的平等性,userChoice并不是纸上谈兵。因此,在另一种情况下,既然用户选择不是石头,它只能是剪刀。听到这个消息太好了!祝你好运。很高兴听到你这么说!祝你好运