UNB/ CS/ David Bremner/ teaching/ cs2613/ labs/ Lab 11

Before the lab

Background


Discussion

Time
10 minutes
Activity
Group discussion

Deep Comparison

Time
25 minutes
Activity
Exercise from book

Follow the Deep Comparison Exercise to write a function deepEqual that passes the following tests. Jasmine uses something very similar for the toEqual matcher used below. Coming from Racket this is a bit disappointing, since the built in equal? function can already do these kinds of comparisons.

describe("equal", function () {
    let obj = {here: {is: "an"}, object: 2};
    it("self", function () {
        expect(deepEqual(obj,obj)).toBe(true);
    });
    it("null", function () {
        expect(deepEqual(null,null)).toBe(true);
    });

    it("different", function () {
        expect(deepEqual(obj, {here: 1, object: 2})).toBe(false);
    });
    it("equivalent", function () {
        expect(deepEqual(obj, {here: {is: "an"}, object: 2})).toBe(true);
    });
});

Variadic Functions

Aside
10 minutes
Activity
Demo/Discussion

Traditional JavaScript supports the arguments object, which is something like an array of all the arguments passed to the function.

function makemap (){
    let map={};
    if (arguments.length %2 != 0)
        return map;

    for (let i=0; i<arguments.length; i+=2) {

    }

    return map;

}

ES2015 supports rest arguments similar to those in Racket. These are more general than the arguments object, and generally preferred.

function brag(name,...args){
    console.log(name + " has");
    for (let i=0; i<args.length; i++) {
        console.log("\t"+args[i]);
    }
}

ES2015 also supports default values for function parameters

function win(person,thing="a brick"){
    console.log(person + " won " + thing);
}

Robot I: Representing a graph

Time
25 minutes
Activity
Writing Tests

Robot II: Representing State

Time
25 minutes
Activity
individual
let first = new VillageState(
  "Post Office",
  [{place: "Post Office", address: "Alice's House"}]
);
let next = first.move("Alice's House");
console.log(next.place);
// → Alice's House
console.log(next.parcels);
// → []
console.log(first.place);
// → Post Office

Use a new suite (describe) labelled "VillageState".

Before you leave