3.1a - Conditionals

Review

In Unit 2 we:

  • Learned how to create and use new projects in our online development environment, Replit

  • Created some basic HTML documents and used CSS to style elements on the page

  • Learned the basics of programming: variables, mathematics, output

News

  • In the 2.7 project I misspelled "Fahrenheit". I accidentally spelled it "Farenheit".

  • When using a variable, you only need to define it once. The following will cause an error:
    let x = 5;

console.log(x);

let x = 30;

Today's Lesson: 3.1 - Conditionals

So you can store a value in a variable, use and manipulate that value, print it, and perform mathematics with it. But how do we make decisions based on a value in a variable?

Friend: "Want to go to the movies?"

You: "Only on the condition that you pay."

What happens if your friend pays? What happens otherwise?


Consider a program that determines if a number is even or odd.
Ask the user for a number.
What if they enter a word?
Okay, let's say it's a number... Now what?

Slideshow for today's lesson

Important Notes & Resources:

Today's Task(s) / Homework

  • Working with a partner - on a piece of paper, whiteboard, or similar, write out the steps you would take (in almost plain English) to play the following game:

    • A secret random number from 1 to 10 is chosen and saved in a variable called secret. The user gets two guesses to find the secret number. If the first guess is incorrect, they are given two hints: too high or low and whether the secret is an even or odd number. Assume you know how to get a random number or exit the game.

  • Complete the project listed in Replit for lesson 3.1 - Conditionals

Extra

  • Here's something weird - try ("A" < "a") or ("A" > "a")

  • Because the if-else condition is boolean logic, you can combine multiple conditions with AND and OR

    • AND uses the operator && and OR uses the operator ||

    • Example:

let x = 5, y = 3;

if ((x < 10) && (y > 1)) {

console.log("Hooray!");

} else {

console.log("Boooo");

}

What will the above code output to the console?