Instructions
This exam is individual, all answers must be your own work.
Save your answers to a directory
tests/quiz2
in yourcs2613
git repository. Commit and push to https://vcs.cs.unb.ca before 10:20AM. If you havegit
problems, ask for help before the deadline.This exam is open book. You can use
Local copies of Eloquent JavaScript, the NodeJS API, and the MDN.net JavaScript documentation which can can be found at
Other parts of the course web site
Your own notes and work from previous labs and assignments.
Questions
For this quiz you will write JavaScript function eval
. This
function is similar to the function of Assignment 3, except that
it evaluates an array where the first element specifies the operator.
For example, consider the following test:
test("+", (t) => assert.strictEqual(eval(['+', 6, 9]),15));
Passing
To get 6 marks (roughly a “C”), your solution must
- Not modify the input list.
- Have full test coverage.
- Work for any expression using “+”, “-”, or “*” with 2 expression or number arguments. In particular it should pass the following tests:
const six_plus_nine = ['+', 6, 9];
const six_times_nine = ['*', 6, 9];
const compound1 = ['+', six_times_nine, six_plus_nine];
const compound2 = ['*', six_times_nine, compound1];
const compound3 = ['-', compound2, 3];
test("+", (t) => assert.strictEqual(eval(six_plus_nine),15));
test("compound1", (t) => assert.strictEqual(eval(compound1),69));
test("compound2", (t) => assert.strictEqual(eval(compound2),3726));
test("compound3", (t) => assert.strictEqual(eval(compound3),3723));
Full marks
To get full marks, your solution must
Have reasonable coding style, including indentation and naming.
Work for any expression using “+”, “-”, or “*” with arbitrary number of expression or number arguments. In particular your code should pass the following additional tests
const plus_list = ['+', 6, 9, 10, 9, -1, -2, -3, -4];
const times_list = ['*', 6, 9, 9, 2];
const compound4 = ['+', plus_list , times_list, 7];
test("new +", (t) => assert.strictEqual(eval(plus_list),24));
test("new *", (t) => assert.strictEqual(eval(times_list),972));
test("compound4", (t) => assert.strictEqual(eval(compound4),1003));