From 5edaee4269bd5e96499a78fb1287bb2f779a08aa Mon Sep 17 00:00:00 2001 From: Valeriy Date: Fri, 26 Apr 2019 22:55:52 +0300 Subject: [PATCH] fix(curriculum): throws is not defined --- .../define-a-primitive-data-type.english.md | 68 +++++++------------ 1 file changed, 23 insertions(+), 45 deletions(-) diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/define-a-primitive-data-type.english.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/define-a-primitive-data-type.english.md index d7dd024aa6..196c7f13ad 100644 --- a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/define-a-primitive-data-type.english.md +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/define-a-primitive-data-type.english.md @@ -26,33 +26,33 @@ it should throw a TypeError with an error message of 'Not a N ```yml tests: - text: Num should be a function. - testString: assert(typeof Num === 'function', 'Num should be a function.'); + testString: assert(typeof Num === 'function'); - text: new Num(4) should return an object. - testString: assert(typeof (new Num(4)) === 'object', 'new Num(4) should return an object.'); - - text: new Num(\'test\') should throw a TypeError with message \'Not a Number\'. - testString: assert(throws(() => new Num('test'), TypeError, 'Not a Number'), 'new Num(\'test\') should throw a TypeError with message \'Not a Number\'.'); - - text: new Num(0) should throw a TypeError with message \'Out of range\'. - testString: assert(throws(() => new Num(0), TypeError, 'Out of range'), 'new Num(0) should throw a TypeError with message \'Out of range\'.'); - - text: new Num(-5) should throw a TypeError with message \'Out of range\'. - testString: assert(throws(() => new Num(-5), TypeError, 'Out of range'), 'new Num(-5) should throw a TypeError with message \'Out of range\'.'); - - text: new Num(10) should throw a TypeError with message \'Out of range\'. - testString: assert(throws(() => new Num(11), TypeError, 'Out of range'), 'new Num(10) should throw a TypeError with message \'Out of range\'.'); - - text: new Num(20) should throw a TypeError with message \'Out of range\'. - testString: assert(throws(() => new Num(20), TypeError, 'Out of range'), 'new Num(20) should throw a TypeError with message \'Out of range\'.'); + testString: assert(typeof (new Num(4)) === 'object'); + - text: new Num('test') should throw a TypeError with message 'Not a Number'. + testString: assert.throws(() => new Num('test'), TypeError, 'Not a Number'); + - text: new Num(0) should throw a TypeError with message 'Out of range'. + testString: assert.throws(() => new Num(0), TypeError, 'Out of range'); + - text: new Num(-5) should throw a TypeError with message 'Out of range'. + testString: assert.throws(() => new Num(-5), TypeError, 'Out of range'); + - text: new Num(10) should throw a TypeError with message 'Out of range'. + testString: assert.throws(() => new Num(11), TypeError, 'Out of range'); + - text: new Num(20) should throw a TypeError with message 'Out of range'. + testString: assert.throws(() => new Num(20), TypeError, 'Out of range'); - text: new Num(3) + new Num(4) should equal 7. - testString: assert.equal(new Num(3) + new Num(4), 7, 'new Num(3) + new Num(4) should equal 7.'); + testString: assert.equal(new Num(3) + new Num(4), 7); - text: new Num(3) - new Num(4) should equal -1. - testString: assert.equal(new Num(3) - new Num(4), -1, 'new Num(3) - new Num(4) should equal -1.'); + testString: assert.equal(new Num(3) - new Num(4), -1); - text: new Num(3) * new Num(4) should equal 12. - testString: assert.equal(new Num(3) * new Num(4), 12, 'new Num(3) * new Num(4) should equal 12.'); + testString: assert.equal(new Num(3) * new Num(4), 12); - text: new Num(3) / new Num(4) should equal 0.75. - testString: assert.equal(new Num(3) / new Num(4), 0.75, 'new Num(3) / new Num(4) should equal 0.75.'); + testString: assert.equal(new Num(3) / new Num(4), 0.75); - text: new Num(3) < new Num(4) should be true. - testString: assert(new Num(3) < new Num(4), 'new Num(3) < new Num(4) should be true.'); + testString: assert(new Num(3) < new Num(4)); - text: new Num(3) > new Num(4) should be false. - testString: assert(!(new Num(3) > new Num(4)), 'new Num(3) > new Num(4) should be false.'); - - text: (new Num(5)).toString() should return \'5\' - testString: assert.equal((new Num(5)).toString(), '5', '(new Num(5)).toString() should return \'5\''); + testString: assert(!(new Num(3) > new Num(4))); + - text: (new Num(5)).toString() should return '5' + testString: assert.equal((new Num(5)).toString(), '5'); ``` @@ -72,46 +72,24 @@ function Num (n) { - - ## Solution
- ```js function Num(n) { - const num = Math.floor(n); - if (isNaN(num)) { + if (isNaN(n)) { throw new TypeError('Not a Number'); } - if (num < 1 || num > 10) { + if (n < 1 || n > 10) { throw new TypeError('Out of range'); } - this._value = num; + this._value = +n; } Num.prototype.valueOf = function() { return this._value; }; Num.prototype.toString = function () { return this._value.toString(); }; - -function throws(func, errorType, msg) { - let hasThrown = false; - let errorMsg = ''; - let correctType = false; - try { - func(); - } - catch (e) { - hasThrown = true; - errorMsg = e.message; - if (e instanceof errorType) { - correctType = true; - } - } - return hasThrown && correctType && msg === errorMsg; -} - ```