Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active March 18, 2024 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MisterBrash/25af83dbde70586f8346f78931fb8ba5 to your computer and use it in GitHub Desktop.
Save MisterBrash/25af83dbde70586f8346f78931fb8ba5 to your computer and use it in GitHub Desktop.
ICS3U Lesson 1.5 - Conditionals (Part 2)

1.5 - Conditionals (Part 2)

ICS3U - Mr. Brash 🐿️ (Click here for Part 1)

The Lesson

The if-else statement allows us to make decisions with code:

if (lives == 0) {
    console.log("Game over!")
}

But what if we needed to make several decisions based on fairly complex conditions?

For example - the user selects a colour based on a number:

let selection = prompt("Enter 1 for red, 2 for blue, 3 for green, 4 for purple")

if (selection == 1) {
    // Do something
}

if (selection == 2) {
    // Do something else
}

if (selection == 3) {
    // Do something else
}

if (selection == 4) {
    // Do something else
}

In the above example, the code is going to check the variable selection FOUR times, even if the user entered "1" or a number not in the list, like "8". That's a lot of wasted checking.

The if-statement has two more optional pieces that help with this scenario.

The else if block will only be checked if the condition above it was false.
Let's retry our colour selection example:

let selection = prompt("Enter 1 for red, 2 for blue, 3 for green, 4 for purple")

if (selection == 1) {
    // Do something
} else if (selection == 2) {
    // Do something else
} else if (selection == 3) {
    // Do something else
} else if (selection == 4) {
    // Do something else
} else {
    // for all other cases, do this
}

Notice in the code above, if the user enters "2", the code will check if it's equal to 1 (it's not), then check if it's equalt to 2 (it is) and it will NOT check any of the other conditions - it will run the block of code for == 2.

Also notice the final else statement. What if the user enters 99? The else block catches all other scenarios (anything that wasn't caught within the conditions).

Note:

  • You can have as many else if statements as you need but only one if.
  • You can only have one else statement. It is optional and it must be last.

Final Notes:

  • To get the length of a String, we can use .length
    For Example:
    let name = "Mr. Squirrel"
    console.log("Your name is", name.length, "characters long.")
  • The internet is full of tutorials, more tutorials, and examples about the if-else statement since it is one of the very first constructs people learn in programming.

🐿️

1.5 - Conditionals (Part 2)

ICS3U - Mr. Brash 🐿️

Your Task:

Let's see if you can follow directions.

Using the prompt() function whenever you need input, do the following:

  1. Ask the user for their name and store it in user_name.

  2. Prompt "Hello <user_name> how old are you? " where <user_name> is the value of the variable. Store the value in age and convert it to a number.

  3. If they are 16 or older ouptut "You are old enough to drive.". Otherwise, output "You are not old enough to drive yet.".

  4. Now we're going to display a menu. Menus are a good example of using if-statements to control the flow of a program. Copy and paste the following to your code to display a menu:

// Show menu
console.log("1 - Play\n2 - Options\n3 - DLC\n4 - Check for Updates\n5 - Exit");

// Ask for selection
let selection = parseInt(prompt("Hi " + user_name + ". Please make a selection "));

Based on what the user selects, output the following:

Value Output
1 "Let's play!"
2 "You selected Options."
3 "No new DLC at this time."
4 "Everything is up to date."
5 "Bye!"

If the user selects anything else, output "Invalid selection."

🐿️


Here's an optional challenge:

You can combine conditions with "AND" or "OR". For example, to check if a number is between 5 and 10:

if ((x >= 5) && (x <= 10)) {
  // Do something
}

In that example, it is checking if the value is greater than or equal to 5 and less than or equal to 10. The symbol for and is double ampersands, &&. The symbol for or is double pipes, || (the key is above the enter key on your keyboard).

  • Ask the user to enter an hour between 0-23 (military time). Just the hour, no minutes. Depending on their entry, output "Good morning!" (hour is 0-11), "Good afternoon!" (hour is 12-17), or "Good evening!" (hour is 18-23). If the hour is any other value, output "Invalid hour!".

Here's another optional challenge:

  • Math.random() creates a random number between 0 and 1. For example, it might give you 0.42564821 or some nonsense like that. Multiplying by 10 will give us a single-digit value above zero: 4.2564821
    We can chop the decimals off by using Math.floor( ) or Math.trunc( ):
let random_number = Math.random() * 10;     // Get a single-digit value (with decimals)
random_number = Math.floor(random_number);  // Chop off the decimals

// Optionally, parseInt will also work:
random_number = parseInt(random_number);

Generate a random single-digit value and give output based on the following:

  • If the value is zero, say so
  • If the value is even, say so
  • If the value is divisible by 2 AND 3, say so
  • If the value is prime, say so

Keep in mind, those are not ELSE-IF situations.

🐿️


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment