Appendix. Exercise answers

Lesson 4

A1:

{
  let DEFAULT_START = 0;
  let DEFAULT_STEP  = 1;

  window.mylib.range = function (start, stop, step) {
    let arr = [];

    if (!step) {
      step = DEFAULT_STEP;
    }

    if (!stop) {
      stop = start;
      start = DEFAULT_START;
    }

    if (stop < start) {
      // reverse values
      let tmp = start;
      start = stop;
      stop = tmp;
    }

    for (let i = start; i < stop; i += step) {
      arr.push(i);
    }

    return arr;
  }
}

Lesson 5

A1:

{ const DEFAULT_START = 0; const DEFAULT_STEP = 1; window.mylib.range = function (start, stop, step) { const arr = []; if (!step) { step = DEFAULT_STEP; } if (!stop) { stop = start; start = DEFAULT_START; } if (stop < start) { // reverse values const tmp = start; start = stop; stop = tmp; } for (let i = start; i < stop; ...

Get Get Programming with JavaScript Next now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.