Permalink
Browse files

Subtract feature

  • Loading branch information...
allenevans committed Dec 24, 2015
1 parent 2f2df9a commit bf4b20b1fc3ccfb2d4e8060a1764eb2c55c1e7d3
Showing with 44 additions and 4 deletions.
  1. +27 −2 calc.js
  2. +8 −1 package.json
  3. +9 −1 spec/calc.spec.js
View
29 calc.js
@@ -14,11 +14,36 @@ function sum(num1, num2) {
return num1 + num2;
}
/**
* Subtracts one number from the other.
* @param {number} num1
* @param {number} num2
* @returns {number}
*/
function subtract(num1, num2) {
return num1 - num2;
}
var num1 = argv._[0];
var num2 = argv._[1];
var operator = argv._[1];
var num2 = argv._[2];
if (isNaN(num1) || isNaN(num2)) {
throw 'Arguments must be valid numbers';
}
console.log(sum(num1, num2));
if (['+', '-'].indexOf(operator) < 0) {
throw 'Invalid operator';
}
var result;
switch(operator) {
case '+':
result = sum(num1, num2);
break;
case '-':
result = subtract(num1, num2);
break;
}
console.log(result);
View
@@ -6,6 +6,12 @@
"scripts": {
"test": "mocha --colors spec/**/*.spec.js"
},
"pre-commit": {
"silent": true,
"run": [
"test"
]
},
"keywords": [
"git",
"precommit",
@@ -23,6 +29,7 @@
"sinon": "^1.17.2"
},
"devDependencies": {
"sinon-chai": "^2.8.0"
"sinon-chai": "^2.8.0",
"pre-commit": "^1.1.2"
}
}
View
@@ -24,7 +24,7 @@ describe('calc app', function () {
});
it('should add two numbers together', function () {
setProcessArgs(3, 4);
setProcessArgs(3, '+', 4);
require('../calc');
expect(console.log).to.have.been.calledWith(7);
@@ -35,4 +35,12 @@ describe('calc app', function () {
expect(function () { require('../calc'); }).to.throw();
});
it('should subtract two numbers', function () {
setProcessArgs(8, '-', 5);
require('../calc');
expect(console.log).to.have.been.calledWith(3);
});
});

0 comments on commit bf4b20b

Please sign in to comment.