Summer of Cyber

Follow me on GitHub

Learn to Code Day 2

Welcome back!

techwomen

Play with the following codepen! By the end of the day, you’ll be able to build your very own! But first we need to learn some basics

See the Pen Little Alchemy Engine Example by mariavanv (@mariavanv) on CodePen.


Now let’s learn a bit about logic! We use logic all the time, even if you don’t realize it.

Example 1: Umbrella Uncertainty

Let’s say you’re getting ready to leave for the day, and you’re deciding what to take with you for the day.

umb

If it’s raining, and you plan on walking, you would bring an umbrella, but if you don’t have to walk (or it’s not raining), then you would not bring an umbrella. This can be represented with a truth table!

I have to walk It’s raining Bring an umbrella
No No No
No Yes No
Yes No No
Yes Yes Yes

The table above can also be represented using 0’s and 1’s (0 is no/false, and 1 is yes/true). In the scenario, imagine A is having to walk, B is if it’s raining, an O (output) is whether or not you need to bring an umbrella.

A B O
0 0 0
0 1 0
1 0 0
1 1 1

This scenario is an example of Logical AND, because both A AND B must be true for the output to be true.

Exercise: Can you think of another example of Logical And?

Example 2: Nurse or Not?

Now let’s look at another example of everyday logic: going to the nurse

nurse

If you have a headache or a stomach ache, your teacher sends you to the nurse. If you have both a headache AND a stomachache, your teacher will also send you to the nurse. Let’s say A is having a headache, B is having a stomach ache, and O is going to the nurse. Then we can represent the scenario with the following table:

A B O
0 0 0
0 1 1
1 0 1
1 1 1

This is an example of Logical Or, because either A or B (or both) need to be true for the output to be true.

Exercise: Can you think of another example of Logical Or?

Example 3: Logical Lateness

Let’s try another example: Being Late for School alarm

Let A be your alarm going off, and let O be being late to school. If your alarm goes off, you will arrive at school on time (not late), but if your alarm doesn’t go off, you will be late for school. We can represent this scenario with the following table:

A O
0 1
1 0

This is an example of Logical Not, because A must not be true for O to be true.

Exercise: Can you think of another example of Logical Not?

Identification Exploration

Now try to identify the following scenarios as AND, OR, or NOT

Scenario 1:

A is if you took notes in class

B is if you studied for the test

O is if you pass the class or not

You will pass the test if you either study or took notes, or both

Scenario 2:

A is a plant gets watered

B is the plant gets sunshine

O is whether or not the plant grows

A plant needs to both be watered and get sunshine in order to grow.

Scenario 3:

A is Eating a sandwich

O is you are hungry

If you do eat a sandwich, you won’t be hungry, and if you do not eat the sandwich, you will be hungry

Logic Gates

A really cool way that engineers represent these concepts are through diagrams called “logic gates”. A really cool logic gate simulator can be found here. There are a lot of circuit parts in the side bar, but the only ones that we learned about today are the and, or & not.

An example of an “OR” gate on Logic.ly

Now, let’s learn about JavaScript