More Practice with JavaScript
CS 130: Conditionals, Lists, �Objects, Loops, & Event Handlers
1
Announcements
2
Goals for Today
3
Outline
4
Takeaways from Tutorial 6
5
Tutorial 6: Code Review
6
Outline
7
Exercises from Lecture 12
Let’s try doing the following exercises:
8
01-color-mixer > If / Else Statements
9
if (condition) {
statement 1;
statement 2;
} else {
statement 1;
statement 2;
}
CONDITION�Boolean expression that evaluates to True or False.
�If the condition evaluates to True, the first block executes.
�If the condition evaluates to False, the second block executes.
01-color-mixer > Logical Operators
10
Operator | Meaning | Explanation |
&& | and | If both operands are true, then the “and expression” also evaluates to true. Otherwise, the “and expression” evaluates to false. |
|| | or | If either or both of the operands are true, then the “or expression” also evaluates to true. Otherwise, the “or expression” evaluates to false. |
! | not | if the operand is false, then the “not” of the operand is true (and vice versa). |
Exercises from Lecture 12
Let’s try doing the following exercises:
11
Objects: Representing Complex Entities
Some kinds of data (i.e. data about people, places, and belongings) are complex, and need more complex data structures to represent them in ways that a computer can understand and easily store an manipulate.
Often entities that are made up of sub-entities, which are related in some way. Examples:
12
Example
const person = {
name: "Jane",
pic: "http://knight.gamebanana.com/img/ico/sprays/patrick_star_preview_2.png",
score: 300
};
13
Code Example: Object Demo
14
Exercises from Lecture 12
Let’s try doing the following exercises:
15
for...of loop versus forEach method
If you want to do something for each item of a list, you have many options. Here are 2 of them (they do the same thing):
const names = ['charlie', 'freddie', 'lucy'];
//option 1:
for (const name of names) {
console.log(name);
}
//option 2:
names.forEach(name => {
console.log(name, idx);
});
16
Before we do 05-list-of-objects...
const people = [
{ name: "Jane", pic: "http://knight.gamebanana.com/patrick.png", score: 300 },
{ name: "Brenda", pic: "https://3.bp.blogspot.com/spongeBob.png", score: 10} ,
{ name: "Wanda", pic: "https://3.bp.blogspot.com/spongeBob.png", score: 60 },
{ name: "Maria", pic: "http://knight.gamebanana.com/patrick.png", score: 80 },
{ name: "Jasper", pic: "https://3.bp.blogspot.com/spongeBob.png, score: 600 },
{ name: "Malik", pic: "http://knight.gamebanana.com/patrick.png", score: 40 }
];�
17
Outline
18
Let’s download and work through HW3 together
19