From 11a3a6bb94e21d8b909b016b65bde459f9ec8daf Mon Sep 17 00:00:00 2001 From: StPig Date: Thu, 29 Oct 2015 20:47:15 +0800 Subject: [PATCH] homework --- calculator.js | 13 ++++++++++++- index.html | 2 ++ main.js | 6 ++++++ test/test_calculator.js | 16 +++++++++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/calculator.js b/calculator.js index caf34db..9368b28 100644 --- a/calculator.js +++ b/calculator.js @@ -1,5 +1,5 @@ function isNumber(numStr){ - var re = /^[0-9\.]+$/; + var re = /^[0-9\.\-]+$/; return re.test(numStr); } @@ -11,5 +11,16 @@ function substract(num1, num2){ return num1 - num2; } +function multiplication(num1, num2){ + return num1 * num2; +} + +function division(num1, num2){ + return num1 / num2; +} + module.exports.add = add; +module.exports.substract = substract; +module.exports.multiplication = multiplication; +module.exports.division = division; module.exports.isNumber = isNumber; diff --git a/index.html b/index.html index cc6a9d9..e5fbd57 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,8 @@

Silly Calculator

=
diff --git a/main.js b/main.js index 957a234..835fc79 100644 --- a/main.js +++ b/main.js @@ -16,6 +16,12 @@ window.onload = function(){ else if (operator == "substract"){ document.getElementById('ans').value = substract(num1, num2); } + else if (operator == "multiplication"){ + document.getElementById('ans').value = multiplication(num1, num2); + } + else if (operator == "division"){ + document.getElementById('ans').value = division(num1, num2); + } else { alert("Bad operator!") } diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..2a498b3 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -4,7 +4,21 @@ var cal = require('../calculator.js') describe('Calculator', function(){ describe('add', function(){ it('add', function(){ - assert.equal('2', cal.add(1, 1)); + assert.equal('123', cal.add(54, 69)); + }) + it('substract', function(){ + assert.equal('-12', cal.substract(46, 58)); + }) + it('multiplication', function(){ + assert.equal('1036', cal.multiplication(28, 37)); + }) + it('division', function(){ + assert.equal('91', cal.division(1547, 17)); + }) + }) +describe('Unvalid Input', function(){ + it('not a number', function(){ + assert.equal(false, cal.isNumber('Hello')) }) }) })