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..c46aeab 100644 --- a/test/test_calculator.js +++ b/test/test_calculator.js @@ -1,10 +1,73 @@ 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('addexponential', function () { + it('add', function () { + assert.notEqual('8', cal.add(0, 2 ^ 3)); + }) + }) + }) + 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)); + }) + }) }) - }) })