Before the lab
Background
The first half of the lab references Eloquent JavaScript Chapter 4.
The second half of this lab lab is based on Chapter 7 of Eloquent JavasScript.
You will also need ES2015 classes, which were introduced in L10
Discussion
- Time
- 10 minutes
- Activity
- Group discussion
- Questions about Assignment 3?
- Questions about Lab 10?
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
Make a directory
~/fcshome/cs2613/labs/L11
Create a file
~/fcshome/cs2613/labs/L11/village.js
, and copy the code from Meadowfield- For your journal: what is
const
?
- For your journal: what is
run
jasmine init
in that directory to create an appropriate jasmine configuration.export
roadGraph
fromvillage.js
and add a suite (i.e.describe
) of two tests tospec/village.spec.js
- Check that the destinations adjacent to "Alice's House" are
["Bob's House","Cabin","Post Office"]
. Make sure your test still passes if the order of adjacent places changes. - use
jasmine.objectContaining
to check that the list of places adjacent to "Alice's House" contains "Bob's House".
- Check that the destinations adjacent to "Alice's House" are
Robot II: Representing State
- Time
- 25 minutes
- Activity
- individual
Copy the class
VillageState
from Eloquent JavaScriptConvert the following code from Eloquent JavaScript to tests in
jasmine/village.spec.js
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".
- For your journal:
- what property of the VillageState data stucture is emphasized in this section of the book?
- what "update" operation(s) in Racket does this remind you of?
Before you leave
- Make sure all of your work related to the delivery Robot is committed and pushed before you go. We will continue working with this example next lab.