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

Homework4 100502203 #45

Open
wants to merge 8 commits 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
18 changes: 17 additions & 1 deletion calculator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function isNumber(numStr){
var re = /^[0-9\.]+$/;
var re = /^[+-]?[0-9]+\.{0,1}[0-9]*$/;
return re.test(numStr);
}

Expand All @@ -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;
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ <h1>Silly Calculator</h1>
<select id="operator" size="1">
<option value="add">+</option>
<option value="substract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
<option value="mod">%</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
Expand Down
39 changes: 28 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
9 changes: 9 additions & 0 deletions test/first_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// first test
var assert = require("assert");
var foo = "bar";

describe("First", function() {
it("Test", function() {
assert.equal(foo, "bar");
});
});
59 changes: 57 additions & 2 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
})
})
})
});