diff --git a/calculator.js b/calculator.js index caf34db..f11707d 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 devide(num1, num2){ + return num1 / num2; +} + +function random(){ + return Math.floor((Math.random() * 100) + 1); +} + +// exports method for other modules or files to use module.exports.add = add; module.exports.isNumber = isNumber; +module.exports.substract = substract; +module.exports.multiply = multiply; +module.exports.devide = devide; +module.exports.random = random; \ No newline at end of file diff --git a/index.html b/index.html index cc6a9d9..6812aaf 100644 --- a/index.html +++ b/index.html @@ -18,16 +18,22 @@

Silly Calculator

- - - -
=
- - - +
+ + + +
=
+ + +
+
+ +
diff --git a/main.js b/main.js index 957a234..d149172 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,10 @@ window.onload = function(){ + + document.getElementById('random_btn').onclick = function(){ + + document.getElementById('num1').value = random(); + } + document.getElementById('go_btn').onclick = function(){ var num1Str = document.getElementById('num1').value; var num2Str = document.getElementById('num2').value; @@ -16,6 +22,12 @@ 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 == "devide"){ + document.getElementById('ans').value = devide(num1, num2); + } else { alert("Bad operator!") } diff --git a/package.json b/package.json index d727f6d..20b2f67 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,8 @@ "dependencies": { "mocha": "~2.3.2" }, - "repository":{} + "repository": {}, + "devDependencies": { + "sinon": "^1.17.1" + } } diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..35d03b2 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,10 +1,60 @@ +// to use methods from other files we simply use `require` with path name 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)); + describe('add', function(){ + it('add', function(){ + assert.equal('2', cal.add(1, 1)); + }) + }) + + describe('substract' , function(){ + it('substract', function(){ + assert.equal('3', cal.substract(5, 2)); + }) + }) + /* ------------ Add new feature (× / ÷) ------------ */ + describe('multiply', function(){ + it('multiply', function(){ + assert.equal('20', cal.multiply(4, 5)); + }) + }) + + describe('devide', function(){ + it('devide', function(){ + assert.equal('4', cal.devide(28, 7)); + }) + }) + /* ----------------- negative path ----------------- */ + describe('negativeAdd' ,function(){ + it('negativeAdd', function(){ + assert.notEqual('6', cal.add(2,3)); + }) + }) + describe('negativeSubstract' , function(){ + it('negativeSubstract', function(){ + assert.notEqual('3', cal.substract(5, 1)); + }) + }) + + describe('negativeMultiply', function(){ + it('negativeMultiply', function(){ + assert.notEqual('21', cal.multiply(4, 5)); + }) + }) + + describe('devide', function(){ + it('devide', function(){ + assert.notEqual('5', cal.devide(28, 7)); + }) + }) + /* --------- Add and mock the random number --------- */ + describe('random', function() { + it('random', function() { + sinon.stub(Math, 'random').returns(20); + assert.equal('22', cal.add(Math.random(), 2)); + }) }) - }) -}) +}) \ No newline at end of file