diff --git a/calculator.js b/calculator.js index caf34db..179a131 100644 --- a/calculator.js +++ b/calculator.js @@ -11,5 +11,22 @@ function substract(num1, num2){ return num1 - num2; } +function multiply(num1, num2){ + return num1 * num2; +} + +function divide(num1, num2){ + return num1 / num2; +} + +function getRandom() { + return Math.floor((Math.random()*100)+1); +} + + module.exports.add = add; module.exports.isNumber = isNumber; +module.exports.substract = substract; +module.exports.multiply = multiply; +module.exports.divide = divide; +module.exports.getRandom = getRandom; \ No newline at end of file diff --git a/index.html b/index.html index cc6a9d9..9aabd8e 100644 --- a/index.html +++ b/index.html @@ -17,17 +17,23 @@ +

Silly Calculator

=
- +
+
+ +
diff --git a/main.js b/main.js index 957a234..e16bf6b 100644 --- a/main.js +++ b/main.js @@ -16,8 +16,17 @@ window.onload = function(){ else if (operator == "substract"){ document.getElementById('ans').value = substract(num1, num2); } + else if (operator == "multiply") { + document.getElementById('ans').value = multiply(num1, num2); + } + else if (operator == "divide") { + document.getElementById('ans').value = divide(num1, num2); + } else { alert("Bad operator!") } } + document.getElementById('getRandom').onclick = function () { + document.getElementById('num1').value = getRandom(); + } } diff --git a/package.json b/package.json index d727f6d..3160cfb 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,10 @@ "dependencies": { "mocha": "~2.3.2" }, - "repository":{} + "repository": {}, + "devDependencies": { + "mocha": "^2.3.3", + "mocha-sinon": "^1.1.4", + "sinon": "^1.17.1" + } } diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..1c3cb5a 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,10 +1,60 @@ var assert = require('assert'); var cal = require('../calculator.js') +var sinon = require('sinon') describe('Calculator', function(){ describe('add', function(){ it('add', function(){ - assert.equal('2', cal.add(1, 1)); + assert.equal('12', cal.add(10, 2)); }) }) + + describe('substract',function () { + it('substract',function () { + assert.equal('8',cal.substract(10,2)); + }) + }) + + describe('multiply',function () { + it('multiply',function () { + assert.equal('20',cal.multiply(10,2)); + }) + }) + + describe('divide',function () { + it('divide',function () { + assert.equal('5',cal.divide(10,2)); + }) + }) + + describe('negative_add',function () { + it('negative_add',function () { + assert.notEqual('1',cal.add(10,2)); + }) + }) + + describe('negative_substract',function () { + it('negative_substract',function () { + assert.notEqual('1',cal.substract(10,2)); + }) + }) + + describe('negative_multiply',function () { + it('negative_multiply',function () { + assert.notEqual('1',cal.multiply(10,2)); + }) + }) + + describe('negative_divide',function () { + it('negative_divide',function () { + assert.notEqual('6',cal.divide(10,2)); + }) + }) + + describe('random_num',function (){ + it('random_num',function () { + sinon.stub(Math, 'random').returns(78); + assert.equal('80',cal.add(Math.random(),2)); + }) + }) })