CS 105

Week 3

Review: Variables

Variables store data in a computer's memory for later use.

All data in a variable has a data type.

Numbers

var n = 42;
var m = 4.2;
var o = 0.42;

Strings

var s = "Hello, world!";
var t = "ILLINOIS";
var u = "14";  // 14, as a String

Functions

var f = function(arg1) {
  return (arg1 + 1);
};
// Function call: f(3)

var g = function(arg1, arg2) {
  return arg1 * arg2;
};
// Function call: g(4, 5)

Arrays

var a = [1, 2, 3, 4, 5];
var b = ["Hello", "World"];
var c = [10, "after", 11, "o'clock"];

Review: Encapsulating Blocks

If Statements

var x = getSomeValue();
if (x < 10) {
  ...
}

If Statements

var x = getSomeValue();
if (x < 10) {
  ...
} else {
  ...
}

For Loops

for (var i = 0; i < 10; i++) {
  ...
  // Runs 10 times
  ...
}

Functions

var f = function(a, b, c, d) {
  ...
};

Pre-Puzzle

Pre-Puzzle #2

Puzzle #1

var d = 0;
for (var i = 0; i < 5; i++) {
  d = d + i;
}
A) 0
B) 5
C) 10
D) 15

Puzzle #2

var k = 17;
var a = [3, 4, 5];

for (var i = 0; i < a.length; i++) {
  k = k – a[i];
}

Puzzle #2

A) 2
B) 3
C) 4
D) 5
E) 6

Puzzle #3

var cart = [ 8.50, 52.49, 31.99 ];

Get _____% off orders of $ __________ or more!

OR

Get _____% off orders of $ __________ or more!

Puzzle #3

var calculate = function (cart) {








}

Dot Operator

console.log("Hello, world!");

Dot Operator

var s = "Hello";
var l = s.length;     // 5
var c = s[1];         // "e"
var c2 = s.charAt(4); // "o"

var a = [2, 4, 6, 8, 10];
var len = a.length;  // 5
var n = a[2];        // 6

Puzzle #4

var s = "Hello, world!";
var x = s.length;

Puzzle #5

var a = [2, 4, 6, 8, 10];
var y = a.length;

Puzzle #6

var s2 = "Hello, world!";
var z = s2.charAt(4);
      // Same as s2[4]

Number Systems

&

Place Value

Place Value of 123

  102 = 100  |  101 = 10  |  100 = 1
-------------|------------|----------
Place        |            |
Value:  100  |        10  |        1
             |            |
Digit:    1  |         2  |        3

             +            +

Place Value of 213

  102 = 100  |  101 = 10  |  100 = 1
-------------|------------|----------
Place        |            |
Value:  100  |        10  |        1
             |            |
Digit:    2  |         1  |        3

             +            +

Our Number System

Our number system uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. We call this system base 10 or the decimal number system.


Computers use the digits 0 and 1. We call this system base 2 or the binary number system.

Place Value of 1012

   22 =      |   21 =     |   20 =
-------------|------------|----------
Place        |            |
Value:       |            |
             |            |
Digit:    1  |         0  |        1

             +            +

Place Value of 0112

   22 =      |   21 =     |   20 =
-------------|------------|----------
Place        |            |
Value:       |            |
             |            |
Digit:    1  |         0  |        1

             +            +

Puzzle #9

Bits and Bytes

We call a single binary digit a bit.
It stores either the value 0 or 1.

Byte

Eight bits make up one byte.


0000 00002 == 010

1111 11112 == 25510

Application

In a computer, colors are represnted by an array of three bytes: red, green, and blue.
(Primary colors of light)


SimpleImage

Looking Forward