Event Objects and Conditional Code
Recap
Housekeeping
- Achievement 4: If This, Then That
1. Conditional code blocks
The if/else statement is used to conditionally run code when an expression (code that returns a value) is true.
Materials
Demo: Greeter with language support
2. Forms: Event.preventDefault()
Demo: Tip Calculator with <select> menu and Event.preventDefault()
- JS Activity: Tip Calculator with drop down menu
- starter code
Breakout Activity
In pairs, take the form created in the demo, using if, else if and/or else statements, set the tipRate based on the service level selected in the menu.
- Note: The value of the select menu is stored in the
form.service.valueproperty.
3. Equality and Truthiness
Coercion in untyped languages (such as Javascript) produces a concept known as "Truthiness": some values are more equal than others, based on their value type.
Materials
Lab Time
- JS Activity: Refactor Adding Machine to create Simple Calculator
- JS Activity: Tip Calculator with drop down menu
- Question: Why does
3 > 2 > 1evaluate tofalse?