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
Event.preventDefault()
2. Forms:
<select>
menu and Event.preventDefault()
Demo: Tip Calculator with - 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.value
property.
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 > 1
evaluate tofalse
?