UNB/ CS/ David Bremner/ teaching/ cs2613/ tests/ sample2

Questions

For this (sample) quiz you will write a JavaScript function strings. This function is to traverse the argument and report all array elements or object fields that are JavaScript string values. Several examples are given below, as tests.

Good Solution

To get 7 marks (roughly a “B”), your solution should

  1. Work for arbitrary JavaScript Array inputs, including nested arrays.
  2. Return the strings in a depth first order. See the tests for some examples of what this means.
  3. Have full test coverage.
  4. Have reasonable indentation and variable names.

In particular your function should pass the following tests.

test("empty array", (t)=> assert.deepEqual(strings([]),[]));
test("single element array", (t)=> assert.deepEqual(strings(["bob"]),
                                                    ["bob"]));
test("mixed strings and numbers", (t)=>
    assert.deepEqual(strings([1,"bob",2,"alice"]), ["bob","alice"]));
test("nested arrays", (t)=>
    assert.deepEqual(strings([1,"bob",[2,"alice"]]), ["bob","alice"]));
test("deeper nesting", (t)=>{
    const expr = [ '*', [ '*', 6, 9 ], [ '+', [ '*', 6, 9 ],
                                         [ '+', 6, 9 ] ] ];
    assert.deepEqual(strings(expr), ['*','*','+','*','+']);
});

Full marks

For full marks, your solution should

  1. Work on arbitrary JavaScript Objects.

  2. Include a (documented) test for an important class of input not tested with the given tests.

Your solution should pass at least the following additional tests.

test("empty object", (t)=> assert.deepEqual(strings({}),[]));
test("single field object",
     (t)=> assert.deepEqual(strings({ name: "bob"}),["bob"]));
test("mixed string and number fields",
     (t)=>assert.deepEqual(strings({age: 1, parent1: "bob",
                                    height: 2, parent2: "alice"}),
                           ["bob","alice"]));
test("nested objects",
     (t)=>assert.deepEqual(strings({ key: {receiver: "bob",
                                           other: {age: 2,
                                                   sender: "alice"} }}),
                           ["bob","alice"]));
test("deeper nesting",
     (t)=>{
         const expr = { op: '*', l: { op: '*', l: 6, r: 9 },
                        r: { op: '+', l: { op: '*', l: 6, r: 9 },
                             r: { op:'+', l: 6, r: 9 } } };
         assert.deepEqual(strings(expr), ['*','*','+','*','+']);
     });