CS 105

Week 5, Part 2

Midterm 1

Midterm 1 is Tuesday, March 3rd, 8:00pm-9:30pm

Six days from now!

Conflict signup on course website! MUST sign up by Friday if you have a conflict.

Midterm 1

25-35 multiple choice questions
2-4 free response questions

Midterm 1 TA Review

This Sunday (Mar. 1)
3:00pm-5:00pm
1000 Lincoln Hall

What to study?

Re-do MPs

Re-do Labs

Lecture Examples

Lecture Videos

Practice Midterms

General Topics

Variables*

Conditionals

For-Loops

Functions

Variable Types

Numbers

Strings (.charAt(), .length, [])

Boolean

Functions

Arrays (.length, [])

Objects (.length)

One New Method

Arrays

If you have a variable storing an array, you can use .push(___) to add whatever is the parameter given to the push method to the end of the array.

Array Push

var arr = [];

arr.push(4);    // [ 4 ]
arr.push("Hi"); // [ 4, "Hi" ]
arr.push(9);    // [ 4, "Hi", 9 ]
arr.push(arr);
[4, "Hi", 9, [4, "Hi", 9]]

Arrays of Objects

Often, we will want to work with arrays of objects

var receipt = [
  { item: "coat", price: 139.99 },
  { item: "gloves", price: 44.99 },
  { item: "hat", price: 22.54 }
];
var receipt = [
  { item: "coat", price: 139.99 },
  { item: "gloves", price: 44.99 },
  { item: "hat", price: 22.54 }
];

How do we access "gloves"?

A) receipt.gloves
B) receipt[0].gloves
C) receipt[0][1]
D) receipt[1][0]
E) receipt[1].item

Example 1

Suppose we want to take a 20% discount on items over $50 and return an array of objects where each object returned has the following format:

{ item: "coat", origPrice: 139.99, newPrice: 111.99, discount: 0.2 }

Lets code!

Example 2

Suppose we now shop around and collect a list of prices for each item:

var receipt = [
  { item: "coat",
    prices: [139.99,129.99,149.99] },
  { item: "gloves",
    prices: [44.99,52.99,39.99] },
  { item: "hat",
     prices: [22.54,39.99,34.99] }
];

Example 2

Suppose we now want to return an object of arrays with the following format for each object returned:

{ item: "coat",
  cheapestPrice: 129.99 }

Lets code!