From 3c0c54ea84fed38da322bd26222622d124a8f29a Mon Sep 17 00:00:00 2001 From: rockwyc992 Date: Wed, 4 Nov 2015 00:50:28 +0800 Subject: [PATCH 1/2] multiple & division feature && fix some bug of regex in isNumber --- calculator.js | 24 ++++++++++++++++++++---- index.html | 2 ++ main.js | 10 ++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/calculator.js b/calculator.js index caf34db..a2280d6 100644 --- a/calculator.js +++ b/calculator.js @@ -1,15 +1,31 @@ -function isNumber(numStr){ - var re = /^[0-9\.]+$/; +function isNumber(numStr) +{ + var re = /^-?[0-9]+(\.[0-9]+)?$/; return re.test(numStr); } -function add(num1, num2){ +function add(num1, num2) +{ return num1 + num2; } -function substract(num1, num2){ +function substract(num1, num2) +{ return num1 - num2; } +function multiple(num1, num2) +{ + return num1 * num2; +} + +function division(num1, num2) +{ + return num1 / num2; +} + module.exports.add = add; +module.exports.substract = substract; +module.exports.multiple = multiple; +module.exports.division = division; module.exports.isNumber = isNumber; diff --git a/index.html b/index.html index cc6a9d9..e755246 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,8 @@

Silly Calculator

=
diff --git a/main.js b/main.js index 957a234..33d4301 100644 --- a/main.js +++ b/main.js @@ -12,11 +12,13 @@ window.onload = function(){ var operator = document.getElementById('operator').value; if (operator == "add"){ document.getElementById('ans').value = add(num1, num2); - } - else if (operator == "substract"){ + } else if (operator == "substract") { document.getElementById('ans').value = substract(num1, num2); - } - else { + } else if (operator == "multiple") { + document.getElementById('ans').value = multiple(num1, num2); + } else if (operator == "division") { + document.getElementById('ans').value = division(num1, num2); + } else { alert("Bad operator!") } } From 50fb188742897b7e74198c4893bfb0b6827e989c Mon Sep 17 00:00:00 2001 From: rockwyc992 Date: Wed, 4 Nov 2015 00:50:48 +0800 Subject: [PATCH 2/2] test case --- test/test_calculator.js | 105 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..c4cf58e 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -3,8 +3,109 @@ var cal = require('../calculator.js') describe('Calculator', function(){ describe('add', function(){ - it('add', function(){ - assert.equal('2', cal.add(1, 1)); + it('0~9 + 0~9', function(){ + for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + assert.equal(i+j, cal.add(i, j)); + } + } + }) + }) + + describe('add', function(){ + it('1000 random case', function(){ + for (var i = 0; i < 1000; i++) { + var x = Math.random(); + var y = Math.random(); + assert.equal(x+y, cal.add(x, y)); + } + }) + }) + + describe('substract', function(){ + it('0~9 - 0~9', function(){ + for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + assert.equal(i-j, cal.substract(i, j)); + } + } + }) + }) + + describe('substract', function(){ + it('1000 random case', function(){ + for (var i = 0; i < 1000; i++) { + var x = Math.random(); + var y = Math.random(); + assert.equal(x-y, cal.substract(x, y)); + } + }) + }) + + describe('multiple', function(){ + it('0~9 * 0~9', function(){ + for (var i = 0; i < 10; i++) { + for (var j = 0; j < 10; j++) { + assert.equal(i*j, cal.multiple(i, j)); + } + } + }) + }) + + describe('multiple', function(){ + it('1000 random case', function(){ + for (var i = 0; i < 1000; i++) { + var x = Math.random(); + var y = Math.random(); + assert.equal(x*y, cal.multiple(x, y)); + } + }) + }) + + describe('division', function(){ + it('1~9 / 1~9', function(){ + for (var i = 1; i < 10; i++) { + for (var j = 1; j < 10; j++) { + assert.equal(i/j, cal.division(i, j)); + } + } + }) + }) + + describe('division', function(){ + it('1000 random case', function(){ + for (var i = 0; i < 1000; i++) { + var x = Math.random(); + var y = Math.random(); + assert.equal(x/y, cal.division(x, y)); + } + }) + }) + + describe('Is number?', function(){ + it('Positive', function(){ + assert.equal(true, cal.isNumber('123.456')); + assert.equal(true, cal.isNumber('-789.012')); + assert.equal(true, cal.isNumber('345')); + assert.equal(true, cal.isNumber('-678')); + assert.equal(true, cal.isNumber('0.901')); + assert.equal(true, cal.isNumber('-0.234')); + assert.equal(true, cal.isNumber('0567')); + assert.equal(true, cal.isNumber('-0890')); + }) + }) + + describe('Is number?', function(){ + it('Negative', function(){ + assert.equal(false, cal.isNumber('.12')); + assert.equal(false, cal.isNumber('+123.456')); + assert.equal(false, cal.isNumber('0.')); + assert.equal(false, cal.isNumber('123.456.789')); + assert.equal(false, cal.isNumber('1e10')); + assert.equal(false, cal.isNumber('1e-10')); + assert.equal(false, cal.isNumber('-1e10')); + assert.equal(false, cal.isNumber('-1e-10')); + assert.equal(false, cal.isNumber('0xabcdef')); }) }) })