CS 105

Week 5

CS 105's
Hunger Games


Solo JavaScript Project
+
Course-Wide Contest

Hunt
Slack

Wade

Hunt

________

Hunt

Wade

Hunt

________

Slack

Wade

Slack

________

Slack

Develop a player to compete in CS 105's Hunger Games!

var player = {
  name: "Hue the Hunter",

  makeDecision:
    function(me, partner, capital) {
      return "h";
    }
};
Live Contest Results

Variables and Data Types

var num = 42;               // Number
var s = "Hello!";           // String
var a = [1, 2, 3];          // Array
var f = function() { ... }; // Function
var o = { a: 1, b: 2 };     // Object

Using Strings

var s = "Hello";    // String
var len = s.length;  // ???

A) 0
B) 1
C) 4
D) 5

Using Strings

var s = "Hello";    // String
var c = s[1];        // ???

A) "H"
B) "e"
C) "l"
D) "o"

var a = [ [52, 3], [44, 0],
          [14, 48], [27, 25] ];
var len = a.length;  // ???

A) 0
B) 3
C) 4
D) 5

var a = [ [52, 3], [44, 0],
          [14, 48], [27, 25] ];
var elem = a[1];

A) [52, 3]
B) [44, 0]
C) [52]
D) [3]
E [44]

Adding to Arrays

There is one last method that we will use in CS 105 when working with Arrays:

var a = [];
a.push("Anything");  // ["Anything"]
a.push(42);          // ["Anything", 42]

.push(value) adds value to the end of the array

Adding to Arrays

var oddNumbers = [];
oddNumbers.push(1);  // [1]
oddNumbers.push(3);  // [1, 3]
oddNumbers.push(5);  // [1, 3, 5]
oddNumbers.push(7);  // [1, 3, 5, 7]

Adding to Arrays

var oddNumbers = [];
for (var i = 0; i < 100; i++) {
  oddNumbers.push( (i*2) + 1 );
}

// [1, 3, 5, 7, 9, ..., 197, 199]

CS 105 Store!

var cart = [
  { item: "light coat", price: 100.00 },
  { item: "leather gloves",
    price: 50.00 },
  { item: "cs105 sticker", price: 0.99 }
];

CS 105 Store!

addItem("light coat", 100.00);
addItem("leather gloves", 50.00);
addItem("cs105 sticker", 0.99);

CS 105 Store!

JS Fiddle

CS 105 Store!

var cart = [];
// Declare the function:


  // Create the object:


  // Add the object to the cart array:


If an item is over $ _____________, take ____% off!

 

If the grand total is over $ _____________, take ____% off!

// Loop through the Array


  // Store final item price:








Puzzle #5

var cart = [
  { item: "light coat", price: 100.00,
    discountPrice:              },

  { item: "leather gloves", price: 50.00,
    discountPrice:              },

  { item: "cs105 sticker", price: 0.99,
    discountPrice:              }
];

Puzzle #6

Write a JavaScript function that returns the total price of the cart, applying any grand total discounts before returning the value.

var weatherData = [
  { week: 1,
    temps: [82, 84, 85, 72, 73, 75, 79]
  },
  { week: 2,
    temps: [84, 87, 89, 89, 76, 78, 81]
  },
  { week: 3,
    temps: [83, 74, 72, 71, 72, 74, 76]
  }
];
var weatherData = [

  { week: 1, highTemp:            },

  { week: 2, highTemp:            },

  { week: 3, highTemp:            }

];

CS 105 Midterm Exam

Oct 12, 2015
7:30pm - 9:00pm