Logic


Slides

Slides Here!


Additional Info


In Class Exercises (ICEs)

Part 1: Creating logical expressions from English verbiage

Go through and for each question try to come up with a logical expression that says when the written text is true

Example:

Given the following English text:
The toast is done when the toaster goes 'ding' or when the toast itself is nicely browned and not smoking

You might write out the following logical expression:
The toast is done when:
(the toaster goes 'ding')
OR ( (the toast is nicely browned) AND  NOT (the toast is smoking)  )

Exercises:
  1. Given:
    I'll be finished with my homework when I've done problems #1, #2, and #3
    Write out a logical expression that completes the following:
    I'll be finished with my homework when:
  2. Given:
    I'm going to flee the building if the building floods, lightning strikes the building, or the building starts to collapse
    Write out a logical expression that completes the following:
    I'm going to flee the building if:
  3. Given:
    My child is ready to leave the house in the morning when they've made their bed, used the restroom, eaten all his/her breakfast, and is not stalling.
    Write out a logical expression that completes the following:
    My child is ready to leave the house in the morning when:
  4. Given:
    We're going to leave when the game is over.  Unless we sit here for more than 10 minutes, in which case we'll leave then.
    Write out a logical expression that completes the following:
    We're going to leave when:

    (You could 'translate' the previous ones mostly by re-arranging the phrases in the sentences and putting in AND/OR/NOT.  For this one you'll need to re-write things a bit more)
  5. Given:
    Take the bread out of the oven when the inside of the loaf hits 208 degrees; don't leave the bread in for more than 40 minutes.
    Write out a logical expression that completes the following:
    Take the bread out of the oven when:
  6. Given:
    Imagine that you're driving on an interstate highway.  Before we pull over and take a break we're going to drive for an hour.  After that, we'll stop at the first exit that has gas or has a restroom.
    Write out a logical expression that completes the following:
    We're going to stop when:
  7. Given:
    Your dinner is complete if you have a Caesar Salad or a house salad, along with a slice of pizza or a chunk of lasagna.
    Write out a logical expression that completes the following:
    Your dinner is complete if:

Part 2: Modifying the provided example file

Save yourself your own copy of the 'Logical Operators and and JavaScript demonstration files, so that you can modify them for this excise.

Once you've done that, make the following changes to the file.  Make sure to stop and review your changes so that you can really get the ideas and the code into your head solidly.

  1. First, change the ranges that the page is checking for.  For example, instead of saying that anything in the range of 20-40 is 'close', you could change the page so that it says that anything in the range 15-37 is 'close'.
    Make sure that you change the overall of acceptable numbers from 1-100 to being -100 to +200.
  2. Add a second number to check for.  When you're done 30 is still a correct answer, but 170 will also be a correct answer.
  3. Change that page so that in addition to saying 20-40 is 'close' (or 15-37, or whatever range you've got), the page will also say that 150-180 is 'close'.
    Do this using a separate, stand-alone else if for now.
  4. Comment out the else if from the prior step, then combine that second range (from step 3) in with the original 'it's close' check.  When you're done you'll have a single else if checking for both ranges

Part 3: Logical ops w/ radio buttons and check boxes

Do the following:

  1. Download a copy of radio_checkbox_starter.html so that you can modify it
  2. The basic idea is that the user will choose option A or option B, check off whichever tasks they've finished, and then click on the button at the bottom of the form.  The page then either tells the user that they've finished all the work they need to do or else tells them that they're not done yet.

    The user is done if they choose option A and checked off BOTH both Task #1 and Task #2, or if they choose Option B and have done at least one of Task #3 and Task #4 (or both).
  3. In order to complete this exercise you must write this check into a single if/else (NOT a multiway if/else).  This is being done in order to force you to use both the AND and OR operators in a single expression.

 

Part 4: Using the switch Statement

Create a page with a textbox that allows the user to enter a month, along with a button to call a function named displayNumberOfDaysInTheMonth.  In the function, you should use a switch statement to display the number of days in the month entered by the user (using an on-page paragraph - do NOT use alert).  Use the switch's default case to provide a message when the user has not entered a valid month, and offer a different message if the user elected to cancel the prompt instead of submitting a month.

Note: one way to easily compare strings is to convert the user's input to lowercase, then use all lowercase month names in your switch's cases.  You may need to search online for a JavaScript function that converts a string to lowercase.