CS 105

Week 5

MP3

Build a tic-tac-toe game!

Available on CS 105 Website
Due: Tomorrow, Feb. 24, 8:00pm

Activity 4

Activity 4.2

Write a JavaScript function that is called calculateGPA that takes in a letter grade and returns the GPA for the given letter grade.

Code Editor

Activity 4.3

Write a JavaScript function that is called countFailing that takes in an array of grades and returns the number of failing grades (grades below 70).

Code Editor

Activity 4.5

Write a JavaScript function that is called compare that takes in two array of grades and returns "first" if the first student has a higher average or "second" if the second student has a higher average.

Code Editor

Grades

Grades are up for Labs 0-4, Activity 0-3, MP0.

Midterm 1

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

Eight days from now!

Midterm 1

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

Data Types

Data Types

var num = 43;     // Number
var s = "Hello";  // String
var b = true;     // Boolean
// Function:
var f = function(...) { ... }; 
// Array (of Strings):
var a1 = ["a", "b"];
// Array (of Numbers):
var a2 = [37, -375];  

Objects

The very last basic data type in JavaScript is an object

Objects are are similar to Arrays in that they are a container of other data types.

Often referred to as a JSON (JavaScript Object Notation) or a Dictionary.

Objects

var person = {
   fname: "Wade",
   lname: "Fagen",
   office: "2215 SC",
   areacode: 217
};

Objects are a comma-separated list of name-value pairs, surrounded by curly braces.

Objects

var classroom = {
   room: "Lincoln Hall Theater",
   size: 615
};

Elements inside of objects are accessed by the dot-operator (.).

var a = classroom.size;  // 615

Values can be any data type:

var math = {
  square: function(num) {
    return num * num;
  }
};
var b = math.square(4);  // 16

Objects

Object methods have a special variable called this that refers to itself when inside of a method.

var circle = {
  r: 5,
  getArea: function() {
    return 3.14 * this.r * this.r;
  }
};
var c = circle.getArea();  // 78.5
circle.r = 10;
var x = circle.getArea();  // 314

Try it!

Objects

We have used objects for several weeks, starting with the MP2 (myInstagram).

Example

Suppose we are writing a function to analyze the weather.

Our function has one input parameter, an object, which has two properties. Each property is an array of numbers.

The lows properties contains the low temperatures for several days. The highs properties contains the low temperatures for several days.

Lets code!