diff --git a/calculator.js b/calculator.js
index caf34db..98eeb90 100644
--- a/calculator.js
+++ b/calculator.js
@@ -1,9 +1,9 @@
function isNumber(numStr){
- var re = /^[0-9\.]+$/;
+ var re = /^[+-]?[0-9]+\.{0,1}[0-9]*$/;
return re.test(numStr);
}
-function add(num1, num2){
+function addition(num1, num2){
return num1 + num2;
}
@@ -11,5 +11,16 @@ function substract(num1, num2){
return num1 - num2;
}
-module.exports.add = add;
+function multiply(num1, num2){
+ return num1 * num2;
+}
+
+function divide(num1, num2){
+ return num1 / num2;
+}
+
+module.exports.addition = addition;
+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..08f8f1f 100644
--- a/index.html
+++ b/index.html
@@ -20,8 +20,10 @@
Silly Calculator
=
diff --git a/main.js b/main.js
index 957a234..ad0b712 100644
--- a/main.js
+++ b/main.js
@@ -10,12 +10,18 @@ window.onload = function(){
var num1 = parseFloat(num1Str)
var num2 = parseFloat(num2Str)
var operator = document.getElementById('operator').value;
- if (operator == "add"){
- document.getElementById('ans').value = add(num1, num2);
+ if (operator == "addition"){
+ document.getElementById('ans').value = addition(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("Bad operator!")
}
diff --git a/test/test_calculator.js b/test/test_calculator.js
index 13f6205..bb081dc 100644
--- a/test/test_calculator.js
+++ b/test/test_calculator.js
@@ -3,8 +3,50 @@ var cal = require('../calculator.js')
describe('Calculator', function(){
describe('add', function(){
- it('add', function(){
- assert.equal('2', cal.add(1, 1));
+ it('add_pos', function(){
+ assert.equal('2', cal.addition(1, 1));
+ })
+ it('add_neg', function(){
+ assert.equal('-2', cal.addition(-1, -1));
+ })
+ it('add_dec', function(){
+ assert.equal('0.2', cal.addition(0.1, 0.1));
+ })
+ })
+
+ describe('sub', function(){
+ it('sub_pos', function(){
+ assert.equal('0', cal.substract(1, 1));
+ })
+ it('sub_neg', function(){
+ assert.equal('0', cal.substract(-1, -1));
+ })
+ it('sub_dec', function(){
+ assert.equal('0', cal.substract(0.1, 0.1));
+ })
+ })
+
+ describe('mul', function(){
+ it('mul_pos', function(){
+ assert.equal('4', cal.multiply(2, 2));
+ })
+ it('mul_neg', function(){
+ assert.equal('4', cal.multiply(-2, -2));
+ })
+ it('mul_dec', function(){
+ assert.equal('0.4', cal.multiply(2, 0.2));
})
})
+
+ describe('div', function(){
+ it('div_pos', function(){
+ assert.equal('1', cal.divide(3, 3));
+ })
+ it('div_neg', function(){
+ assert.equal('1', cal.divide(-3, -3));
+ })
+ it('div_dec', function(){
+ assert.equal('1', cal.divide(0.3, 0.3));
+ })
+ })
})