fix(curriculum): throws is not defined
This commit is contained in:
committed by
mrugesh mohapatra
parent
fbf5439248
commit
5edaee4269
@ -26,33 +26,33 @@ it should throw a <code>TypeError</code> with an error message of <code>'Not a N
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>Num</code> should be a function.
|
||||
testString: assert(typeof Num === 'function', '<code>Num</code> should be a function.');
|
||||
testString: assert(typeof Num === 'function');
|
||||
- text: <code>new Num(4)</code> should return an object.
|
||||
testString: assert(typeof (new Num(4)) === 'object', '<code>new Num(4)</code> should return an object.');
|
||||
- text: <code>new Num(\'test\')</code> should throw a TypeError with message \'Not a Number\'.
|
||||
testString: assert(throws(() => new Num('test'), TypeError, 'Not a Number'), '<code>new Num(\'test\')</code> should throw a TypeError with message \'Not a Number\'.');
|
||||
- text: <code>new Num(0)</code> should throw a TypeError with message \'Out of range\'.
|
||||
testString: assert(throws(() => new Num(0), TypeError, 'Out of range'), '<code>new Num(0)</code> should throw a TypeError with message \'Out of range\'.');
|
||||
- text: <code>new Num(-5)</code> should throw a TypeError with message \'Out of range\'.
|
||||
testString: assert(throws(() => new Num(-5), TypeError, 'Out of range'), '<code>new Num(-5)</code> should throw a TypeError with message \'Out of range\'.');
|
||||
- text: <code>new Num(10)</code> should throw a TypeError with message \'Out of range\'.
|
||||
testString: assert(throws(() => new Num(11), TypeError, 'Out of range'), '<code>new Num(10)</code> should throw a TypeError with message \'Out of range\'.');
|
||||
- text: <code>new Num(20)</code> should throw a TypeError with message \'Out of range\'.
|
||||
testString: assert(throws(() => new Num(20), TypeError, 'Out of range'), '<code>new Num(20)</code> should throw a TypeError with message \'Out of range\'.');
|
||||
testString: assert(typeof (new Num(4)) === 'object');
|
||||
- text: <code>new Num('test')</code> should throw a TypeError with message 'Not a Number'.
|
||||
testString: assert.throws(() => new Num('test'), TypeError, 'Not a Number');
|
||||
- text: <code>new Num(0)</code> should throw a TypeError with message 'Out of range'.
|
||||
testString: assert.throws(() => new Num(0), TypeError, 'Out of range');
|
||||
- text: <code>new Num(-5)</code> should throw a TypeError with message 'Out of range'.
|
||||
testString: assert.throws(() => new Num(-5), TypeError, 'Out of range');
|
||||
- text: <code>new Num(10)</code> should throw a TypeError with message 'Out of range'.
|
||||
testString: assert.throws(() => new Num(11), TypeError, 'Out of range');
|
||||
- text: <code>new Num(20)</code> should throw a TypeError with message 'Out of range'.
|
||||
testString: assert.throws(() => new Num(20), TypeError, 'Out of range');
|
||||
- text: <code>new Num(3) + new Num(4)</code> should equal 7.
|
||||
testString: assert.equal(new Num(3) + new Num(4), 7, '<code>new Num(3) + new Num(4)</code> should equal 7.');
|
||||
testString: assert.equal(new Num(3) + new Num(4), 7);
|
||||
- text: <code>new Num(3) - new Num(4)</code> should equal -1.
|
||||
testString: assert.equal(new Num(3) - new Num(4), -1, '<code>new Num(3) - new Num(4)</code> should equal -1.');
|
||||
testString: assert.equal(new Num(3) - new Num(4), -1);
|
||||
- text: <code>new Num(3) * new Num(4)</code> should equal 12.
|
||||
testString: assert.equal(new Num(3) * new Num(4), 12, '<code>new Num(3) * new Num(4)</code> should equal 12.');
|
||||
testString: assert.equal(new Num(3) * new Num(4), 12);
|
||||
- text: <code>new Num(3) / new Num(4)</code> should equal 0.75.
|
||||
testString: assert.equal(new Num(3) / new Num(4), 0.75, '<code>new Num(3) / new Num(4)</code> should equal 0.75.');
|
||||
testString: assert.equal(new Num(3) / new Num(4), 0.75);
|
||||
- text: <code>new Num(3) < new Num(4)</code> should be true.
|
||||
testString: assert(new Num(3) < new Num(4), '<code>new Num(3) < new Num(4)</code> should be true.');
|
||||
testString: assert(new Num(3) < new Num(4));
|
||||
- text: <code>new Num(3) > new Num(4)</code> should be false.
|
||||
testString: assert(!(new Num(3) > new Num(4)), '<code>new Num(3) > new Num(4)</code> should be false.');
|
||||
- text: <code>(new Num(5)).toString()</code> should return \'5\'
|
||||
testString: assert.equal((new Num(5)).toString(), '5', '<code>(new Num(5)).toString()</code> should return \'5\'');
|
||||
testString: assert(!(new Num(3) > new Num(4)));
|
||||
- text: <code>(new Num(5)).toString()</code> should return '5'
|
||||
testString: assert.equal((new Num(5)).toString(), '5');
|
||||
|
||||
```
|
||||
|
||||
@ -72,46 +72,24 @@ function Num (n) {
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='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;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user