Nested conditionals in JavaScript allow for more complex decision-making by placing one conditional statement inside another. This structure enables a program to check additional conditions only if the previous condition is true, creating a layered decision process. In this lesson, students will learn how to implement nested if, else if, and else statements to create more specific control flows in their programs. Through examples and exercises, they will gain a deeper understanding of how to manage multiple conditions effectively, enhancing the logic of their code.
To gain a better understanding about Nested Conditionals and how they work, first we need to understand what condtionals are; Conditionals in general are "if-then" statements. They allow your program to make decisions based on certain conditions. If the condition is true, the program will do one thing; if it’s false, the program will do something else (or nothing at all). Here's an example.
Think of it as asking a question and then deciding what to do based on the answer:
Question: "How are you doing?"
Answer/Input: "Good!"
Result/Output: "Glad to hear that!^^"
Alternative Answer/Input: "Not that well"
Alternative Result: "Sorry to hear that, I hope it isn't that bad."
On the other hand, Nested Conditionals are conditionals within other conditionals. Let's use the previous example and extend it to an example with nested conditionals:
Question: "How are you doing?"
Answer/Input: "Good!"
Result/Output: "Oooh~! Did something fun happen?"
Answer/Input # 2: "Actually, yes, I got an A for an assignment in my CSSE class ^^"
Result/Output # 2: "Ooh nice job! ^^"
Alternative Answer/Input: "Not that well"
Alternative Result: "Sorry to hear that, what happend?"
Answer/Input #2: "I threw up in front of my entire class."
result/output #2: "Oh... That probably sucks..."
Popcorn Hack #1
This code gives the player the option to pick between a red and green apple and decide whether or not to eat it.
1.Try changing the values of appleColour to "green" and eatApple to "no".
2.Try adding a scenario for what would happen if the player found a yellow apple as well. Remember to change to the value of appleColor to "yellow" to get a different result.
```python
%%html
```
How You Can Utilize this in your RPG
There are several things you can use nested conditionals in your RPG for. For example, you can create interactions (like the apple example shown above) or more complex dialogue with other NPC's, that could impact the outcomes and endings of the plot.And since you guys love fighting so much, you could also try to create a combat system in which the player can choose between different actions such as, attacking, defending, fleeing using an item, etc. and depending on the players decision there could be different outcomes. :)