我正在重新学习if/else语句的基础知识,因为它们是我在学校的弱点。
我正在参加代码学院的java脚本课程,我正处于if语句的阶段。我感到困惑的例子是这样的:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
else 语句是否认为用户选择等于"剪刀"?
还是将用户选择切换到"纸张",将计算机选择切换到"摇滚"?
我知道这真的很基本,但它总是让我感到困惑。
整个功能如下
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';
}
}
在这个例子中userChoice
值通常首先被检查,假设它不等于computerChoice
- 在这种情况下,我们检查两个值并将它们相互比较。
因此,在匹配第一个if
块后,正在验证第二个匹配,即找到computerChoice
的值。
让我们假设以下场景:userChoice = 'scissors'
我们需要检查computerChoice
的价值是多少。如果computerChoice
的价值不是"剪刀",那么我们知道它不会是平局。我们必须寻找另一场比赛。
我们找到了与第一个条件匹配的块:
if (userChoice === 'scissors'){}
现在我们需要看看这个if
语句中的内容,我们有:
if (computerChoice === "rock"){
return 'The computer won!';
} else {
return 'you won!';
}
这意味着如果computerChoice = 'rock'
,那么它将return 'The computer won!'
如果computerChoice
不是"rock",则else
语句将运行,该语句将return 'you won!'
。
重要说明:代码从上到下执行(几乎没有例外(,因此首先检查此块:
if(userChoice === computerChoice){
return 'It is a tie!';
}
如果将此块移动到函数的底部,则可能会看到意外的结果。
const determineWinner = (userChoice, computerChoice) => {
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!';
}
}
//found the match!!!
if (userChoice === 'scissors'){
if (computerChoice ==="rock"){
return 'The computer won!';
} else {
//run else statement
return 'you won!';
}
}
//it did not reach this point and returned before :(
if(userChoice === computerChoice){
return 'It is a tie!'; //should be a tie
}
if (userChoice === 'bomb'){
return 'Bomb beats all';
}
}
console.log(determineWinner('scissors', 'scissors'))
我想强调这种行为,以便您牢记这一点。
我希望这是有道理的。
这基本上是你的if
语句所做的:
if (computerChoice === 'paper') {
return 'The computer won!';
} if (computerChoice !== 'paper') {
return 'You won!';
}
如果if
语句为 false,则调用else
语句 - 因此,如果computerChoice
是不完全paper
的任何内容,则调用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!';
}
不,不是,这就是第 4 个 IF 语句处理的内容。 其结构化方式意味着每个IF块仅处理两个输入。如果用户把S放在剪刀里,那么它将跳过那个IF块
我会尽量用最好的方式解释它。对于此代码片段:
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!';
} else {
return 'You won!';
}
}
计算机按语句的顺序运行。首先,他检查用户选择了什么(在这个片段中是摇滚(。if (userChoice === 'rock')
然后,由于用户确实选择了 rock,它将移动到检查计算机选择的下一个语句。如果计算机选择了纸张(岩石的正确计数器(,它将移动到语句的返回,即计算机选择了计数器并获胜
if (computerChoice === 'paper') {
return 'The computer won!';
但是,如果计算机选择了纸张以外的任何其他内容,则跳过上面的if语句并执行else语句,即玩家获胜
else {
return 'You won!';
}