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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.