Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active March 7, 2024 00:16
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/301ca555ba725c57eebd7a13561f6ac5 to your computer and use it in GitHub Desktop.
Save MisterBrash/301ca555ba725c57eebd7a13561f6ac5 to your computer and use it in GitHub Desktop.
ICS3U Lesson 1.5 - Conditionals (Part 1)

1.5 - Conditionals (Part 1)

ICS3U - Mr. Brash 🐿️

Quick Review

  • We can give output to the screen with console.log()
    • Each call to console.log() puts a new line
    • We can put multiple items separated with + or ,
  • We can get input by using prompt()
    • We need to declare the prompt function at the top of our code
    • const prompt = require("prompt-sync")();
  • We can store values in variables by using the let keyword
    • Variables can be reused without needing let again

You can jump ahead to the task, if you want.

The if Statement

One of the most used structures in programming is the if-else statement. Every programming language has it (or something just like it).

It works like this:

if (some_sort_of_condition) {
  // The code to run if the condition was true
}

This is how we make decisions like:

if (number_of_lives == 0) {
  console.log("Game Over!");
}

Notice the double-equal sign? ==

  • A single equal sign is the assignment operator: x = 5 makes x the number 5.
  • A double equal sign is the check operator to ask the question "Is it true"?
Operator Meaning Example
= Assignment let name = "Mr. Squirrel"
== Is Equal
Returns true or false
name == "Mr. Squirrel"
!= Is NOT Equal
Returns true or false
name != "Mr. Brash"
> Is Greater
Returns true or false
x > 10
>= Is Greater or Equal
Returns true or false
x >= 10
< Is Less Than
Returns true or false
Math.PI < 10
<= Is Less Than or Equal
Returns true or false
Math.PI <= 3.15

Your Task:

  • Make a new file: 1.5 - Conditionals 1.js

  • We will be using prompt() so add the required line at the top of your code file.

  • Ask the user for their age

    • Don't forget, input is a String, not a number, so you'll have to convert it.
    • if they are 50 years old or older, print to the screen "You qualify for the senior discount!"
    • if they are younger than 16, print "You're not old enough to drive yet."
  • Now ask the user the user for their name and store the answer in a variable called user_name

    • if the user_name is "Mr. Squirrel" print the squirrel emoji: "🐿️"
    • if the user_name is longer than 7 characters print "You have a long name."
      • You can read how to get the length of a string here.
  • Now ask the user how long their name is (how many characters)

    • If the number they enter matches the length of user_name print "That's correct! ✔️"
    • If the number they enter is higher than the length of the name, print "Too high ✖️"
    • If the number they enter is lower than the length of the name, print "Too low ✖️"

A little challenge:

We've seen the basic math operators +, -, *, / but there are more. One important one is the modulo operator %. This gives the remainder of a division statement.

For Example:

let x = 5 % 2
console.log(x)   // The output will be 1

x = 19 % 5
console.log(x)   // The output will be 4

x = 20 % 2
console.log(x)   // The output will be 0

We can use modulo to determine if a value is even or odd by checking the remainder of dividing by 2.

  • Determine if the user's age is even or odd and print that to the screen.
    For Example: "Your age is an even number." or "Your age is an odd number."




🐿️

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