.png)
week 1
LESSON 2
Math + Logic
Your Second Assignment
Objectives:
1. Start thinking like a computer
2. Reintroduce math + logic
​
Now we're going to begin learning about the more left-brained side of the course. We will be presenting logic concepts related to coding that will help you prepare for the coming weeks ahead.
​
​
Declaring and changing variables
Alright, this is for all the people that graduated/left school and thought, "when will I ever need Math again?" Well, that moment is now. So, let's drive into it! I promise it doesn't bite.
​
Let's start from the basics. Here are some cats in Japanese apparel:
Ronin
Jade
Bingo Mango
Sassy Mao
We have Ronin, Jade, Bingo Mango, and Sassy Mao. They're all good friends. Ronin thinks she was a mermaid in a previous life, and wants to be called Ariel. What does Ronin do?
​
Jade: Hi Ronin!
Ronin: My name is not Ronin anymore. It is Ariel.
Jade: Oh. Okay then? Hi Ariel!
Ariel: Hi.
​
What Ronin did there was tell Jade that her name was no longer called Ronin. Instead, she wants to be called Ariel. Fair enough. How does this work in the code world?
​
//declare the variable
var name = "Ronin"
​
//reassign the variable
name = "Ariel"
Ariel
Jade
Bingo Mango
Sassy Mao
Ariel is now very happy with her name. Yay for Ariel.
​
Congratulations, you now know what variable declaration and reassignment means.
​
​
​
​
​
Now, let's get into some Algebra. I love Chess, so here are some Chess pieces:
Algebra Time
Rook
Bishop
Knight
Queen
King
Pawn
In Chess, there are normally 8 Pawns per player
Mel's Pawns
Carson's
Pawns
So, let's say that I am winning like always and Carson only has 2 Pawns left on the board
How many Pawns were taken? Let's write this out:
​
//variable names
var totalNumberOfPawns;
var pawnsLeft;
var pawnsTaken;
​
//variable declaration
totalNumberOfPawns = pawnsLeft + pawnsTaken;
​
// substitute
8 = 2 + pawnsTaken
​
// rearrange equation
pawnsTaken = 8-2
​
pawnsTaken = 6
So, the total pawns taken from Carson is 6. Brilliant. You just learned how to use algebra in Javascript.
Let's Learn Logic!
Computer programming is all about logic. You can call it if/else statements, for loops, while loops, etc.
If statement - If this happens...then this will happen
Say we want our robot to tell our friend "Happy Birthday!" when it is their birthday. On all other days, we want the robot to say "Hi, friend!" How would we create this logic?
​
Let's say your friend's birthday is March 17, and today is April 10. What will the robot say?
​
Naturally, it will say "Hi, friend!"
How would this look like via code?
var today = "April 10"
if(today === "March 17"){
console.log("Happy Birthday!")
} else {
console.log("Hi, friend!")
}
Boolean - True or False?
Let's now look at this example and try to create it using true and false values. How would that look like?
var birthday = true
if(birthday === true){
console.log("Happy Birthday!")
} else {
console.log("Hi, friend!")
}
While loop - While this happens... keep doing this
Let's now look at this example and try to create a while loop instead. How would that look like?
​
var today = "April 10"
while(today === "March 17"){
console.log("Happy Birthday!")
break
}
​
This is case, we decided to only say "Happy Birthday!" if it is their birthday and nothing if it is not.
Visit and read: https://www.w3schools.com/js/js_variables.asp
​
​
Visit and read: https://www.w3schools.com/js/js_if_else.asp
​
​
Visit and read: https://www.w3schools.com/js/js_loop_while.asp
​
​
Visit and watch: https://www.youtube.com/watch?v=gI-qXk7XojA
An error occurred. Try again later
Your content has been submitted
