rmurphey / js-assessment-answers

125 stars 84 forks source link

Improved some solutions #19

Closed surtich closed 9 years ago

surtich commented 10 years ago

curryIt solution:

Now, when you call the function, it could take a variable number of arguments. Thus it is not necessary to pass the arguments one by one.

A valid test for the solution might be:

    it('you should be able to curry existing functions', function() {
      var curryMe = function(x, y, z) {
        return x / y * z;
      };

      var a = Math.random(), b = Math.random(), c = Math.random(), result;

      result = answers.curryIt(curryMe);
      expect(typeof result).to.eql('function');

      result = answers.curryIt(curryMe)(a);
      expect(typeof result).to.eql('function');

      result = answers.curryIt(curryMe)(a)(b);
      expect(typeof result).to.eql('function');

      result = answers.curryIt(curryMe)(a)(b)(c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));

      result = answers.curryIt(curryMe, a)(b, c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));

      result = answers.curryIt(curryMe, a, b, c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));

      result = answers.curryIt(curryMe, a, b)(c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));

      result = answers.curryIt(curryMe)(a, b, c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));

      result = answers.curryIt(curryMe)()()(a, b, c);
      expect(typeof result).to.eql('number');
      expect(result).to.eql(curryMe(a, b, c));
    });
surtich commented 10 years ago

memoizeIt

A test for this function wuold be:

it('you should be able to memoize arguments', function() {

      var memoizeMe = function(x, y, z) {
        if (z !== undefined) {
          return x / y * z;
        } else {
          return x * y;
        }
      };

      var a = Math.random(), b = Math.random(), c = Math.random(), result;

      result = answers.memoizeIt(memoizeMe);

      expect(typeof result).to.eql('function');
      expect(result.cache).to.eql({});

      expect(result(a, b)).to.eql(memoizeMe(a, b));
      expect(result.cache[a + ' ' + b]).to.eql(memoizeMe(a, b));

      expect(result(a, b, c)).to.eql(memoizeMe(a, b, c));
      expect(result.cache[a + ' ' + b + ' ' + c]).to.eql(memoizeMe(a, b, c));

    });
surtich commented 10 years ago

inherance section tests

/*jshint expr:true */
/*globals describe:true, it:true, expect:true, beforeEach:true, console:true */
if (typeof define !== 'function') {
  var define = require('amdefine')(module);
}
if (typeof expect !== 'function') {
  var expect = require('expect.js');
}

define([
  'app/inheritance'
  ], function(answers) {
    describe('inheritance', function () {

      it('should extend an object from other whith prototype inheritence', function () {
        var a = Math.random(), b = Math.random(), B = Math.random(), x = Math.random(), y = Math.random(),
        child = {
          b: b,
          x: x,
          z: {
            a: a,
            b: b
          }
        },
        parent = {
          a: a,
          b: B,
          x: {
            a: a
          },
          y: {
            b: b
          }
        },
        c = child,
        p = parent;
        answers.deepExtend(c, p);
        expect(parent === p).to.eql(true);
        expect(child === c).to.eql(true);      
        expect(p.a).to.eql(a);
        expect(c.a).to.eql(a);
        expect(c.b).to.eql(B);
        expect(typeof c.z).to.eql('object');
        expect(c.z.a).to.eql(a);
        expect(c.x).to.eql(p.x);
        expect(c.x.a).to.eql(a);
        expect(c.x === p.x).to.eql(false);
        expect(c.y).to.eql(p.y);
        expect(c.y === p.y).to.eql(false);

      });

      it('should extend a class from other whith classical inheritence', function () {
        var a = Math.random(), b = Math.random(), B = Math.random(), x = Math.random(), y = Math.random();

        function Parent(a, b) {
          this.a = a;
          this.b = b;
        }

        Parent.prototype.getA = function() {
          return this.a;
        };

        Parent.prototype.getB = function() {
          return this.b;
        };

        Parent.prototype.getY = function() {
          return y;
        };

        function Child(a, b, x) {
          Parent.prototype.constructor.apply(this, [a, b]);
          this.x = x;
        }

        answers.inherits(Child, Parent);

        Child.prototype.getX = function() {
          return this.x;
        };

        var child = new Child(a, b, x),
            child2 = new Child(b, a, x);

        expect(child instanceof Object).to.eql(true); //Trivial
        expect(child instanceof Child).to.eql(true);
        expect(child instanceof Parent).to.eql(true);
        expect(child.constructor === Child).to.eql(true);
        expect(Object.getPrototypeOf(child) === Child.prototype).to.eql(true);
        expect(Child.prototype.constructor === Child).to.eql(true);

        expect(child.x).to.eql(x);
        expect(child.a).to.eql(a);
        expect(child.getA()).to.eql(a);
        expect(child.x).to.eql(x);
        expect(child.getX()).to.eql(x);
        expect(child.__super__ !== undefined).to.eql(true);

        expect(child2.a).to.eql(b);
        expect(child.getY === child2.getY).to.eql(true);
        expect(child.getY()).to.eql(child2.getY());

        Child.prototype.getB = function () {
          return this.__super__.getB.call(this) + B;
        };

        expect(child.getB()).to.eql(b + B);

        expect(child.__super__.getB.call(child)).to.eql(b);

      });

    });
  });
ashleygwilliams commented 9 years ago

hi @IGZJavierPerez ! all of this is really great- but it's quite a large PR. it could be great if you could break these down by test? i.e. a PR for curry, a PR for memoize- that'll make it easier to discuss each one. thanks! closing. ill be on the lookout for new PRs :)