Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKaitlyn Dodds
32,545 PointsCan't figure out why my solution doesn't pass.
It would seem like my solution would work. I have tested it on my own computer and it catches the error nicely.
var expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
let handler = function () { subtraction('some', 'num'); };
expect(handler).to.throw(Error);
expect(handler).to.throw('subtraction only works with numbers!');
})
})
function subtraction (number1, number2) {
if (typeof number1 !== 'number' || typeof number2 !== 'number') {
throw Error('subtraction only works with numbers!')
}
return number1 - number2
}
3 Answers
Jesus Mendoza
23,289 PointsHi Kaitlyn,
If you rewatch the previous video you can see that Guil wraps the function that throws an error inside a function called handler and then executes the test again that function handler:
var expect = require('chai').expect
describe('subtraction', function () {
var subtraction = require('../WHEREVER')
it('only works with numbers', function () {
// YOUR CODE HERE
var handler = function() { subtraction('1', '2'); };
expect(handler).to.throw(Error);
expect(handler).to.throw('subtraction only works with numbers!');
})
})
That should work
Kaitlyn Dodds
32,545 PointsI do have a handler function. The only difference being I used let rather than var...
Kaitlyn Dodds
32,545 PointsI also copied and pasted your answer, and it still doesn't work.
Is this challenge broken?
Jesus Mendoza
23,289 PointsThat's weird. I just tried it and it works perfectly
Jesus Mendoza
23,289 PointsI changed the let
with var
in your code and it works fine