Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -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;
37 changes: 20 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
<!DOCTYPE html>
<!DOCTYPE html>
<html>
<head>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Silly Calculator</title>
<style type="text/css" media="screen">
input, div {
display:inline-block;
}
input {
width: 5em;
}
input, div, button, select{
font-size:large;
}
input, div {
display: inline-block;
}

input {
width: 5em;
}

input, div, button, select {
font-size: large;
}
</style>
</head>
<body>
</head>
<body>
<h1>Silly Calculator</h1>
<input type="text" name="" id="num1" value="1" />
<select id="operator" size="1">
<option value="add">+</option>
<option value="substract">-</option>
<option value="add">+</option>
<option value="substract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
<input type="text" id="ans" value="2" />
<button id="go_btn">Go!</button>

<script src="calculator.js" type="text/javascript" charset="utf-8"></script>
<script src="main.js" type="text/javascript" charset="utf-8"></script>
</body>
</body>
</html>
49 changes: 28 additions & 21 deletions main.js
Original file line number Diff line number Diff line change
@@ -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!")
}
}
}
73 changes: 68 additions & 5 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
@@ -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));
})
})
})
})
})