CS 105

Week 4, Part 2

MP3

Build a tic-tac-toe game!

Available on CS 105 Website by 3:00pm
Due: Tuesday, Feb. 24, 8:00pm

Activity 4

Functions, loops, and other JavaScript!

Available tonight, 5 problems, +2 extra credit problems
Due: Monday, Feb. 23, 9:00am

Grades

Labs so far will be graded generously, if you did the lab you'll earn all the points.

Gradebook online soon, will send e-mail when online.

Midterm 1

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

Two weeks from yesterday!

Where are we in CS 105?

Data Types

Data Types

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

Booleans

Boolean have only two possible values: true or false.

Boolean are the result of comparisons.

var a = 3;
var b = (a < 5);
console.log(b);

Try it!

Booleans

Functions will often return a Boolean value.

/**
 * Returns true if the input
 * parameter (temp) is at or
 * below 32, false otherwise.
 */
var isFreezing = function(temp) {
};

Lets program!

Strings

In our previous coding example, we added strings!

var a = "Hello";
var b = "World";
var c = a + b;  // "HelloWorld"

Adding two strings together will join them into one string. This is called string concatenation.

Averages

Lets average a list one more time...

var a = [ 93, 97, 82 ];

Lets program!

Shortcuts with Numbers

When adding, subtracting, multiplying, or dividing when the result is going into the same variable, you can use a shortcut!

var a = 5;
a += 2;  // Add two to a
         // Same as: a = a + 2
a *= 5;  // Multiply a by five
         // Same as: a = a * 5

Shortcuts with Numbers

var b = 3;
b -= 3;  // Subtracts three from b
         // Same as: b = b - 3
b++;  // Adds one to b
      // Same as: b = b + 1, b += 1
b /= 5;  // Divide b by 5
         // Same as: b = b / 5

Shortcuts with Numbers

x++;     // Adds one to x
x += a;  // Adds the value in a to x

x--;     // Subtracts one from x
x -= a;  // Subtracts a form x

x *= a;  // Multiply value in x by a
x /= a;  // Divide the value in x by a

Average GPA

Suppose we want to calculate our average GPA based off our letter grades.

var grades = ["A", "C", "B+", "A-", "B"];

Lets program!