# 章序号/节序号/节/笔记序号 codecademy, Condition # 概念阐释 - 丰富`if...else` ,可以有更多的条件,让条件可以有多于**2种的可能**。 - 总是位于`if` 和`else`之间。 - `return` 不可以在除函数和switch中使用。 ## 语法 `if`/`else if`/`else` # 举例子 ![](http://image.harryrou.wiki/2023-01-16-%E6%88%AA%E5%B1%8F2023-01-16%2008.39.07.png) - **用编程语言:** - 当stopLight等于red时,计算机评定为`false`,继续向下 - 当stopLight等于yellow时,计算机评定为`true`,blocked,现实结果"slow down" - **用人话:** - 红色停止 - 黄色减速 - 绿色通行 - 没有指示灯,注意!未知 # 类比、比较与对比 [[js condition switch 语句]] # 问题 **1.** *注意:字符串it's Spring 的 ' 怎么写?条件内用的是数学赋值运算符* Let’s create a program that keeps track of the way the environment changes with the seasons. Write a conditional statement to make this happen! In **main.js** there is already an `if...else` statement in place. Let’s add an `else if` statement that checks if `season` is equal to `'winter'`. Inside the code block of the `else if` statement, add a `console.log()` that prints the string `'It\'s winter! Everything is covered in snow.'`. **2.** Add another `else if` statement that checks if `season` is equal to `'fall'`. Inside the code block of the `else if` statement you just created, add a `console.log()` that prints the string `'It\'s fall! Leaves are falling!'`. **3.** Add a final `else if` statement that checks if `season` is equal to `'summer'`. Inside the code block of the `else if` statement you just created, add a `console.log()` that prints the string `'It\'s sunny and warm because it\'s summer!'`. ```js let season = 'summer'; if (season === 'spring') { console.log('It\'s spring! The trees are budding!'); } else { console.log('Invalid season.'); } ``` # 问题答案 [codecademy](https://www.codecademy.com/courses/introduction-to-javascript/lessons/control-flow/exercises/else-if) # 备注(经验集/错误集) 可以用[[js condition switch 语句]]代替 也可以写成函数 ```js function Light(stopLight){ switch(stopLight){ case 'red': return 'stop'; break; case'yellow': return 'slow down'; break; case 'green': return 'go'; break; default: break; } }; console.log(Light('green')); ```