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

Instructions

  1. This exam is individual, all answers must be your own work.

  2. Save your answers to a directory tests/quiz2 in your cs2613 git repository. Commit and push to https://vcs.cs.unb.ca before 10:20AM. If you have git problems, ask for help before the deadline.

  3. This exam is open book. You can use

    1. Local copies of Eloquent JavaScript, the NodeJS API, and the MDN.net JavaScript documentation which can can be found at

      https://www.cs.unb.ca/~bremner/teaching/cs2613/books

    2. Other parts of the course web site

    3. Your own notes and work from previous labs and assignments.

  4. To get 7 marks (roughly a “B”), complete either Q1 or Q2. To get full marks complete both questions. In either case your answers should

    1. Not modify the data structure after it is created.
    2. Have full test coverage.
    3. Have reasonable indentation and variable names.
    4. Only use a constant amount of extra storage (e.g. no copying the whole data structure).
  5. When you are finished, complete the following:

    The last two 7 digit hashes must be identical. If they are not, ask for help.

Questions

For this quiz you will a write JavaScript class List. This class has the following constructor.

constructor(value, rest) {
    this.first = value;
    this.rest = rest;
}

The empty list is represented by the object null, and a simple use of List is as follows

test("object", (t) => {
    let list = new List(42, null);
    assert.strictEqual(list.first,42);
    assert.strictEqual(list.rest,null);
});

Q1: length method

Write an length method that calculates the length (number of values) of a List object. In particular your code should pass the following tests. One solution is to use recursion in a very similar way to Assignment 3.

test("one node",
     (t)=>{
         let list1 = new List("hello",null)
         assert.strictEqual(list1.length(),1);
     });

test("two nodes",
     (t)=>{
         let list2 = new List ("hello", new List("world", null));
         assert.strictEqual(list2.length(),2);
     });

test("big list",
     (t)=>{
         let list3 = null;
         for (let i=0; i<100; i++){
             list3= new List (i,list3);
         }
         assert.strictEqual(list3.length(),100);
     });

Q2: Iterator

Add appropriate methods and classes so that your List is iterable. In particular the following tests should pass.

test("iterator 1",
     (t)=>{
         let list2 = new List ("hello", new List("world", null));
         result="";
         for (let val of list2) {
             result+=val;
         }
         assert.strictEqual(result,"helloworld");
     });

  test("iterator 2",
     (t)=>{
         let list3 = null;
         for (let i=0; i<100; i++){
             list3= new List (i,list3);
         }
         let result = 0;
         for (let val of list3) {
             result+=val;
         }
         assert.strictEqual(result, 4950);
     });