diff --git a/calculator.js b/calculator.js index caf34db..948bd35 100644 --- a/calculator.js +++ b/calculator.js @@ -1,5 +1,5 @@ function isNumber(numStr){ - var re = /^[0-9\.]+$/; + var re = /^[+-]?[0-9]+\.{0,1}[0-9]*$/; return re.test(numStr); } @@ -11,5 +11,21 @@ function substract(num1, num2){ return num1 - num2; } +function multiply(num1, num2){ + return num1 * num2; +} + +function divide(num1, num2){ + return num1 / num2; +} + +function mod(num1, num2){ + return num1 % num2; +} + module.exports.add = add; +module.exports.substract = substract; +module.exports.multiply = multiply; +module.exports.divide = divide; +module.exports.mod = mod; module.exports.isNumber = isNumber; diff --git a/index.html b/index.html index cc6a9d9..d5910f8 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,9 @@

Silly Calculator

=
diff --git a/main.js b/main.js index 957a234..2f665ac 100644 --- a/main.js +++ b/main.js @@ -5,19 +5,36 @@ window.onload = function(){ if (!isNumber(num1Str) || !isNumber(num2Str)){ alert("Some of the input is not a number!") document.getElementById('ans').value = "ERROR" - return + return; } - var num1 = parseFloat(num1Str) - var num2 = parseFloat(num2Str) + 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!") + + switch(operator){ + case "add": + document.getElementById('ans').value = add(num1, num2); + break; + + case "substract": + document.getElementById('ans').value = substract(num1, num2); + break; + + case "multiply": + document.getElementById('ans').value = multiply(num1, num2); + break; + + case "divide": + document.getElementById('ans').value = divide(num1, num2); + break; + + case "mod": + document.getElementById('ans').value = mod(num1, num2); + break; + + default: + alert("Bad operator!"); + break; } } } diff --git a/test/first_test.js b/test/first_test.js new file mode 100644 index 0000000..38ef741 --- /dev/null +++ b/test/first_test.js @@ -0,0 +1,9 @@ +// first test +var assert = require("assert"); +var foo = "bar"; + +describe("First", function() { + it("Test", function() { + assert.equal(foo, "bar"); + }); +}); \ No newline at end of file diff --git a/test/test_calculator.js b/test/test_calculator.js index 13f6205..19ebc50 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -2,9 +2,64 @@ var assert = require('assert'); var cal = require('../calculator.js') describe('Calculator', function(){ + + describe('isNumber', function(){ + it('input is number', function(){ + assert.equal(true, cal.isNumber(513)); + }), + + it('input is not number', function(){ + assert.equal(false, cal.isNumber("asd12")); + }) + }), + describe('add', function(){ - it('add', function(){ + it('add_positive', function(){ assert.equal('2', cal.add(1, 1)); + }), + + it('add_negative', function(){ + assert.equal('-4', cal.add(1, -5)); + }) + }), + + describe('substract', function(){ + it('substract_positive', function(){ + assert.equal('2', cal.substract(3, 1)); + }), + + it('substract_negative', function(){ + assert.equal('6', cal.substract(-1, -7)); + }) + }), + + describe('multiply', function(){ + it('multiply_positive', function(){ + assert.equal('10', cal.multiply(2, 5)); + }), + + it('multiply_negative', function(){ + assert.equal('-15', cal.multiply(-3, 5)); + }) + }), + + describe('divide', function(){ + it('divide_positive', function(){ + assert.equal('2', cal.divide(4, 2)); + }), + + it('divide_negative', function(){ + assert.equal('-5', cal.divide(20, -4)); + }) + }), + + describe('mod', function(){ + it('mod_positive', function(){ + assert.equal('2', cal.mod(5, 3)); + }), + + it('mod_negative', function(){ + assert.equal('-2', cal.mod(-5, -3)); }) }) -}) +});