Skip to content

Instantly share code, notes, and snippets.

@MisterBrash
Last active March 18, 2024 20:54
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/91d89b65479a65a213a5d3f65094811a to your computer and use it in GitHub Desktop.
Save MisterBrash/91d89b65479a65a213a5d3f65094811a to your computer and use it in GitHub Desktop.
ICS3U Lesson 1.6 - Logical Operators (AND and OR) & Modulus

1.6 - Logical Operators & Modulus

ICS3U - Mr. Brash 🐿️

Mr. Brash will demonstrate the following in class. If you miss class, you will need to read and learn independently. Jump to the task.

1. Using Modulus (%) to get the remainder in a division

17 ÷ 5 = 3.4 otherwise known as 3 with a remainder of 2
(because 3×5=15 and 17-15=2)

8 ÷ 4 = 2 with zero remainder
57 ÷ 8 = 7.125 or 7 remainder 1

In code, we use modulus (%) to find just the remainder. Examples:

let my_number = 17;

// Getting the remainder if divided by 5
let rem = my_number % 5;
console.log("The remainder when divided by 5 is: ", rem);

// Determine if the number is even (divisible by 2)
if (my_number % 2 == 0) {
    console.log("That's an even number.");
} else {
    console.log("That's an odd number.");
}

2. Logical operators AND and OR:

  • AND - represented by &&

    The light will turn on if switch A and switch B are turned on.
    if (switch_A == "on" && switch_B == "on")


  • OR - represented by ||

    The light will turn on if switch A OR switch B (or both) are on.
    if (switch_A == "on" || switch_B == "on)


Example 1: Determine if a number is between 5 and 10:

// the user gave us a number and it's stored in "user_number"
if (user_number > 5 && user_number < 10) {
    console.log("Your number is between 5 and 10");
}

Example 2: Is the number below 10 and either 4 or 6:

if (user_number < 10 && (user_number == 4 || user_number == 6)) {
    // Your code here
}

Your Task

Mr. Squirrel has a strange number game he wants you to code. It involves three numbers. Follow the instructions below.

  1. Ask the user if they want to play a game, if they enter affirmatively, play the game.

    Do you want to play a game? (y/n): 
    
  2. Ask the user to enter a number. If they entered a number greater than 10 and less than fifty, tell them their next number should be greater than 50. (it's ok if they don't oblige)

  3. Ask for a second number. If this number is even, tell the user their next number should be odd. If it's odd, tell them their next number should be even. (it's ok if they don't oblige)

  4. Ask for a third and final number.

  5. A set of three numbers is considered Squirrelly if their sum+1 is divisible by 10. Tell the user if their numbers are Squirrelly or not.

  6. A set of three numbers is considered Jazzy if at least one of the numbers is divisible by 3. Tell the user if their numbers are Jazzy or not.

For the game above, we will assume that the user always enters a number when asked. But just thinking about our code - what if they don't?

What do I do when I'm done?

Modify the game a little, as follows:

  • Adjust the original intro question so that it defaults to "y". That is to say that if the user just hits Enter without typing anything, it assumes they entered "y".

    Do you want to play a game? [y]/n:

  • Let's redefine "Jazzy" to be at least two of the numbers are divisible by 3. Change your code to reflect this.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment