Before the lab
- Read about Optional arguments
- Read Chapter 4 of Eloquent JavaScript
Follow the
Deep Comparison Exercise
to write a function deepEqual
that passes the following
tests. assert uses something very similar for the deepStrictEqual
test used below. Your function should pass the following tests.
let obj = {here: {is: "an"}, object: 2};
test("self", (t)=> assert.strictEqual(deepEqual(obj,obj),true));
test("null", (t)=> assert.strictEqual(deepEqual(null,null),true));
test("!null", (t)=> assert.strictEqual(deepEqual(null,obj),false));
test("different", (t)=>
assert.strictEqual(deepEqual(obj, {here: 1, object: 2}),false));
test("equivalent", (t)=>
assert.strictEqual(deepEqual(obj, {here: {is: "an"}, object: 2}),true));
Remember from L09 that you need the following lines to use the test framework
const test=require("node:test");
const assert=require("assert");
JavaScript supports arrays. They are actually implemented as objects, which leads to some slightly surprising behaviour:
> x=[]
> x[10]=1
> x
> x["10"] === x[10]
Follow the (first part of the)
Sum of a Range Exercise
to write a range
function that passes the following tests.
test("empty", (t)=>assert.deepStrictEqual(range(2,1),[]));
test("single", (t)=> assert.deepStrictEqual(range(2,2),[2]));
test("multiple", (t)=>
assert.deepStrictEqual(range(42,50),[42,43,44,45,46,47,48,49,50]));
Follow the (second part of the)
Sum of a Range Exercise
to write a sum
function that passes the following tests.
test("empty", (t)=> assert.strictEqual(sum([]),0));
test("single", (t)=> assert.strictEqual(sum([2]),2));
test("multiple", (t)=> assert.strictEqual(sum(range(1,10)),55));
Add an optional step argument to the range function so that the following tests pass. The original tests should keep passsing, that's a big part of the point of having unit tests.
test("step 2", (t)=> assert.deepStrictEqual(range(1,10,2),[1,3,5,7,9]));
test("step -1", (t)=> assert.deepStrictEqual(range(5,2,-1),[5,4,3,2]));