O'Reilly logo

Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond by JD Isaacks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required