Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 4 - 102502041 #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
17 changes: 14 additions & 3 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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;
}

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;
4 changes: 3 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
<h1>Silly Calculator</h1>
<input type="text" name="" id="num1" value="1" />
<select id="operator" size="1">
<option value="add">+</option>
<option value="addition">+</option>
<option value="substract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
Expand Down
10 changes: 8 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
Expand Down
46 changes: 44 additions & 2 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
})
})
})