From 2cf8a0c214fc11702f988b210bcfe0388e52e543 Mon Sep 17 00:00:00 2001 From: lihungte96 Date: Thu, 29 Oct 2015 22:20:30 +0800 Subject: [PATCH 1/3] lesson4 --- calculator.js | 25 ++++++++++----- index.html | 37 +++++++++++----------- main.js | 49 ++++++++++++++++------------- test/test_calculator.js | 68 ++++++++++++++++++++++++++++++++++++++--- 4 files changed, 129 insertions(+), 50 deletions(-) diff --git a/calculator.js b/calculator.js index caf34db..21f7e55 100644 --- a/calculator.js +++ b/calculator.js @@ -1,15 +1,26 @@ -function isNumber(numStr){ - var re = /^[0-9\.]+$/; - return re.test(numStr); +function isNumber(numStr) { + var re = /^-?[0-9]+.?[0-9]*$/; + return re.test(numStr); } -function add(num1, num2){ - return num1 + num2; +function add(num1, num2) { + return num1 + num2; } -function substract(num1, num2){ - return num1 - num2; +function substract(num1, num2) { + return num1 - num2; +} + +function multiply(num1, num2) { + return num1 * num2; +} + +function divide(num1, num2) { + return num1 / num2; } module.exports.add = add; +module.exports.substract = substract; +module.exports.multiply = multiply; +module.exports.divide = divide; module.exports.isNumber = isNumber; diff --git a/index.html b/index.html index cc6a9d9..0602d0f 100644 --- a/index.html +++ b/index.html @@ -1,34 +1,37 @@ - + - + Silly Calculator - - + +

Silly Calculator

=
- - + diff --git a/main.js b/main.js index 957a234..09caaea 100644 --- a/main.js +++ b/main.js @@ -1,23 +1,30 @@ -window.onload = function(){ - document.getElementById('go_btn').onclick = function(){ - var num1Str = document.getElementById('num1').value; - var num2Str = document.getElementById('num2').value; - if (!isNumber(num1Str) || !isNumber(num2Str)){ - alert("Some of the input is not a number!") - document.getElementById('ans').value = "ERROR" - return +window.onload = function () { + document.getElementById('go_btn').onclick = function () { + var num1Str = document.getElementById('num1').value; + var num2Str = document.getElementById('num2').value; + if (!isNumber(num1Str) || !isNumber(num2Str)) { + alert("Some of the input is not a number!") + document.getElementById('ans').value = "ERROR" + return + } + var num1 = parseFloat(num1Str) + var num2 = parseFloat(num2Str) + var operator = document.getElementById('operator').value; + if (operator == "add") { + document.getElementById('ans').value = add(num1, num2); + } + 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(operator); + alert("Bad operator!"); + } } - var num1 = parseFloat(num1Str) - var num2 = parseFloat(num2Str) - var operator = document.getElementById('operator').value; - if (operator == "add"){ - document.getElementById('ans').value = add(num1, num2); - } - else if (operator == "substract"){ - document.getElementById('ans').value = substract(num1, num2); - } - else { - alert("Bad operator!") - } - } } diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..75f0b15 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,10 +1,68 @@ var assert = require('assert'); var cal = require('../calculator.js') -describe('Calculator', function(){ - describe('add', function(){ - it('add', function(){ - assert.equal('2', cal.add(1, 1)); +describe('Calculator', function () { + describe('add', function () { + describe('addpositive', function () { + it('add', function () { + assert.equal('2', cal.add(1, 1)); + }) + }) + describe('addnagative', function () { + it('add', function () { + assert.equal('3', cal.add(4, -1)); + }) + }) + describe('addonepoint', function () { + it('add', function () { + assert.equal('4', cal.add(2.4, 1.6)); + }) + }) + describe('addpointone', function () { + it('add', function () { + assert.equal('2.511', cal.add(2.4, .111)); + }) + }) + }) + describe('sub', function () { + describe('subtractpositive', function () { + it('substract', function () { + assert.equal('2', cal.substract(4, 2)); + }) + }) + describe('subtractnagative', function () { + it('substract', function () { + assert.equal('6', cal.substract(4, -2)); + }) + }) + describe('subtracttonagative', function () { + it('substract', function () { + assert.equal('-2', cal.substract(2, 4)); + }) + }) + }) + describe('mul', function () { + describe('mulpositive', function () { + it('multiply', function () { + assert.equal('8', cal.multiply(2, 4)); + }) + }) + describe('mulnagative', function () { + it('multiply', function () { + assert.equal('-256', cal.multiply(16, -16)); + }) + }) + }) + describe('div', function () { + describe('divpositive', function () { + it('divide', function () { + assert.equal('8', cal.divide(16, 2)); + }) + }) + describe('divnagative', function () { + it('divide', function () { + assert.equal('-9', cal.divide(27, -3)); + }) + }) }) - }) }) From 8c2493d9eeaf9cd2ea4d93efbaf671f7dea20477 Mon Sep 17 00:00:00 2001 From: lihungte96 Date: Thu, 29 Oct 2015 22:24:55 +0800 Subject: [PATCH 2/3] test add --- test/test_calculator.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test_calculator.js b/test/test_calculator.js index 75f0b15..82f620c 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -23,6 +23,11 @@ describe('Calculator', function () { assert.equal('2.511', cal.add(2.4, .111)); }) }) + describe('addexponential', function () { + it('add', function () { + assert.equal('8', cal.add(0, 2 ^ 3)); + }) + }) }) describe('sub', function () { describe('subtractpositive', function () { From 85e8a46cbd652df11b360a0db38d9e644bd6b1b8 Mon Sep 17 00:00:00 2001 From: lihungte96 Date: Thu, 29 Oct 2015 23:22:42 +0800 Subject: [PATCH 3/3] not equal --- test/test_calculator.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_calculator.js b/test/test_calculator.js index 82f620c..c46aeab 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -25,7 +25,7 @@ describe('Calculator', function () { }) describe('addexponential', function () { it('add', function () { - assert.equal('8', cal.add(0, 2 ^ 3)); + assert.notEqual('8', cal.add(0, 2 ^ 3)); }) }) })