diff --git a/binary/binary.spec.js b/binary/binary.spec.js index c674b074..63ed387c 100644 --- a/binary/binary.spec.js +++ b/binary/binary.spec.js @@ -1,8 +1,11 @@ var Binary = require('./binary'); describe('binary', function() { + it('0 is decimal 0', function() { + expect(new Binary('0').toDecimal()).toEqual(0); + }); - it('1 is decimal 1', function() { + xit('1 is decimal 1', function() { expect(new Binary('1').toDecimal()).toEqual(1); }); @@ -30,8 +33,15 @@ describe('binary', function() { expect(new Binary('10001101000').toDecimal()).toEqual(1128); }); - xit('carrot is decimal 0', function() { + xit('00011111 is decimal 31', function() { + expect(new Binary('00011111').toDecimal()).toEqual(31); + }); + + xit('invalid inputs are decimal 0', function() { expect(new Binary('carrot').toDecimal()).toEqual(0); + expect(new Binary('012').toDecimal()).toEqual(0); + expect(new Binary('10nope').toDecimal()).toEqual(0); + expect(new Binary('nope10').toDecimal()).toEqual(0); }); -}); \ No newline at end of file +}); diff --git a/binary/example.js b/binary/example.js index e4c24002..8174186e 100644 --- a/binary/example.js +++ b/binary/example.js @@ -3,7 +3,11 @@ module.exports = Binary; function Binary(binary) { - this.binary = parseInt(binary, 2); + if (binary.match(/^[01]*$/)) { + this.binary = parseInt(binary, 2); + } else { + this.binary = 0; + } } Binary.prototype.toDecimal = function () {