Conditionals | Decision making in Code

Through introducing conditional statements, this class addresses flowcharting and how it maps algorithmic thinking.

Prep

Morning Activity: Interactivity Treasure Hunt (15 minutes)

  • Find 10 examples of interactivity on web pages.
  • Things to look for:
    • anything with a button
    • sliders
    • carousels
    • things where the cursor is tracked

1. Conditionals

Conditional Slides

Key Takeaways

  • Conditional statements are based in boolean truthy/falsy options
  • an if block doesn’t need and else block
    • the else block is generally used when there is an else if block
  • JS has equality and strict equality
  • any value can be converted to a boolean value

Error Handling and Fail Fast with if statements

  • inside your functions, use simple if statements to check variable types and other conditions and return nothing.
  • This is often used with expressions like:
    • if (isNaN(value)) { return } or if (inputValue.length <= 0) { return}

Switch Statements

Ternary Operators

Another way that you can check conditionals is ternary operators

  • Thes are great for simple in line functions

  • Unlike an if statement, an else condition is required

  • they are often used to check null values

    • ie:
    const greeting = (person) => {
      const name = person ? person.name : 'stranger';
      return `Hello ${name}`
    }

Think it out activity: pseudocode conditional logic

  • Pick one of the tasks from the end of day activity list to pseudo code
  • Plan out your logic as we’ve been practicing
    • identify the objective
    • identify variables
    • outline the logic
    • identify unknowns and uncertains
    • research and test

2. Javascript and Forms

Javascript Forms Slides

Key Takeaways

Today's Achievement

End of Day Activity: Create something using conditional logic

Using a git repo create one of the following. You’ll need to use a mix of forms and conditional code (these are listed from easiest to hardest)

  • An automatic greeter
  • A counter with min and max value limits
  • A calculator with input fields
  • A calculator with buttons