Intro To JavaScript - Week 3
Intro To JavaScript - Week 3
and Loops
Week 3
Course Overview
Current Course Schedule: February 24 to April 21, Thursdays @ 6-7
PM at GDC (check Slack for specific room) and on Zoom (we will
continue to monitor pandemic conditions)
if (condition) {
do_something; code to run if condition
is true
} else {
}
Example of If/Else Statements
Here is an example of an if / else statement.
if (isThirsty) {
buyMoge();
} else {
saveMoney();
}
OR
} else {
do_something_different;
}
List of Conditional Operators
● a == b - tests if the expressions are equal
● a >= b - tests if the first expression is greater or equal to the
second
● a > b - tests if the first expression is greater than the second
● a <= b - tests if the first expression is less or equal to the
second
● a < b - tests if the first expression is less than the second
● a != b - tests if the first expression does not equal the second
● a && b - tests if both expressions evaluate to true (and)
● a || b - tests if either expression evaluates to true (or)
● !== and === - we will look at these later!!!
Examples of Conditional Operators
Example of &&: Example of ||:
function checkGrade(grade) {
if ( (grade >= 80) && (grade < 90) ) {
console.log(“I made a B!”);
} else {
console.log(“I didn’t make a B…”);
}
}
Longer If/Else Statements: Else If
What if instead of just checking if we made a B, we check what letter
grade we made overall? We can do this by creating longer if / else
statements by introducing else if blocks:
if (condition) {
do_something;
} else if (condition) {
do_something_different;
} else if (condition) {
do_something_different2;
} else {
do_something_different3;
}
Longer If/Else Statement Examples
function checkGrade(grade) {
if ( grade >= 90 ) {
console.log(“I made an A!”);
} else if ( (grade >= 80) && (grade < 90) ) {
console.log(“I made a B!”);
} else if ( (grade >= 70) && (grade < 80) ) {
console.log(“I made a C!”);
} else if ( (grade >= 60) && (grade < 70) ) {
console.log(“I made a D!”);
} else {
console.log(“I failed…”);
}
}
Food for Thought
Let’s think more about how we can make decisions based on certain
expressions.
With what we know so far about if / else if statements, how would I write
a function, numberToMonth, that takes in a number from 1 to 12, and
based on the number, prints out the respective month to the console?
}
Food for Thought
The previous function implementation gets the job done, but it can be
cumbersome to write so many else if statements and also negatively
impacts code readability.
function happyBirthday() {
console.log(“Happy Birthday!”);
This gets the job done, but our code suffers from extreme repetition.
We have control over how many times the code inside of loops will
run.
There are three types of loops: for loops, while loops, and do…
while loops. We will cover for and while loops.
Anatomy of a For Loop
Starting
condition with
iteration Terminating How to get
For keyword condition to the end
variable
for (var i = 0; i < count; i++) {
<code here>
}
Note: We will often start our iteration variables at 0. This will make
more sense later in the course!
Example of a For Loop
Let’s see how the following loop will run in practice:
for (var i = 0; i < 2; i++) {
console.log(i);
}
1
2
4
8
...
1024
Anatomy of a While Loop
A while loop will continue to run while its expression is true and will
terminate when the condition is false.
<code here>
}
Example of a While Loop
while (tired) {
sleep();
The while loop will continue to run until tired changes to false.
Another While Loop Example
var newAge = 21;
var i = 0;
while (i < newAge) {
happyBirthday();
i++;
}
The loop will run infinitely, as the expression will never evaluate to false!
We’ve learned a lot of new syntax today - if, else, and else if
statements, switch statements, for loops, while loops, logical
operators, and variable operators!
Let’s walk through how we can put these concepts into practice together
by writing a function, walkToTreasure, that takes in x and y
coordinates on a beach and “walks” to the location of the treasure buried
in the sand!
Homework for This Week
● Write a function named countUpwardsByThree that has no parameters. In this
function, use a for loop or while loop to count from 0 to 30 by 3, and output each
number to the terminal as the loop counts (print 0, print 3, print 6, …, print 30). Call
this function once.
Timers
- How can we delay running our code or run code at certain intervals of time?
Variable Scope
- How can variables be accessed across our program?
- How can we limit the access of variables to certain parts of code?
Code Organization
- How can we maintain good program hygiene?
Attendance