CPNT 262 Day 6 - Booleans, comparison operators and conditionals
Housekeeping
- Quick chat about Fat arrow functions
- No more Node until we start backend on Oct 26
Trophy of the Day
1. Spoiler demo
- Form inputs for Tip Calculator with anonymous functions
2. 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
- Boolean values
- Equality comparisons and sameness
- Comparison operators
- Equality and truthiness terminology
Key Takeaways
-
Use
===when testing for equality (it's safer). Use==once you understand truthiness (or you just can't get the job done with===). -
Any value can be converted to a Boolean value:
myBool = Boolean(myVar); // Method 1
myBool = !!myVar; // Method 2
Activities
- Why does
3 > 2 > 1evaluate tofalse?
3. Conditional code blocks
The if/else statement is used to conditionally run code when an expression is true.
Materials
Key Takeaways
- An
ifblock only runs if its expression evaluates totrue - An
else ifblock only runs if its expression evaluates totrueAND allifandelse ifblocks before it have evaluated tofalse. - An
elseblock only runs if all previousifandelse ifblocks have evaluated tofalse. - The AND logical operator (
&&) only evaluates totrueif both operands also evaluate totrue. - The OR logical operator (
||) evaluates totrueif EITHER of the two operands evaluate totrue. - The NOT logical operator (
!) inverts atruevalue tofalse, and vice versa.
Activities
4. Value validation
A common task in untyped languages is confirming the value type that you're working with.
Materials
- Takeaways: Validating values
Activity
- Using your knowledge of comparison operators and conditional statements, add validation to your Tip Calculator that ensures that:
- The entered Bill Total is greater than $0, and
- The supplied Service Level is one of the options available in the select menu.
Prep
Booleans and Conditional Statements
- Boolean values
- Making decisions in your code - conditionals
- Comparison operators
- Equality comparisons and sameness
