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 102502044 #50

Open
wants to merge 2 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
24 changes: 20 additions & 4 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
function isNumber(numStr){
var re = /^[0-9\.]+$/;
function isNumber(numStr)
{
var re = /^-?[0-9]+(\.[0-9]+)?$/;
return re.test(numStr);
}

function add(num1, num2){
function add(num1, num2)
{
return num1 + num2;
}

function substract(num1, num2){
function substract(num1, num2)
{
return num1 - num2;
}

function multiple(num1, num2)
{
return num1 * num2;
}

function division(num1, num2)
{
return num1 / num2;
}

module.exports.add = add;
module.exports.substract = substract;
module.exports.multiple = multiple;
module.exports.division = division;
module.exports.isNumber = isNumber;
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ <h1>Silly Calculator</h1>
<select id="operator" size="1">
<option value="add">+</option>
<option value="substract">-</option>
<option value="multiple">×</option>
<option value="division">÷</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
Expand Down
10 changes: 6 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ window.onload = function(){
var operator = document.getElementById('operator').value;
if (operator == "add"){
document.getElementById('ans').value = add(num1, num2);
}
else if (operator == "substract"){
} else if (operator == "substract") {
document.getElementById('ans').value = substract(num1, num2);
}
else {
} else if (operator == "multiple") {
document.getElementById('ans').value = multiple(num1, num2);
} else if (operator == "division") {
document.getElementById('ans').value = division(num1, num2);
} else {
alert("Bad operator!")
}
}
Expand Down
105 changes: 103 additions & 2 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,109 @@ var cal = require('../calculator.js')

describe('Calculator', function(){
describe('add', function(){
it('add', function(){
assert.equal('2', cal.add(1, 1));
it('0~9 + 0~9', function(){
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
assert.equal(i+j, cal.add(i, j));
}
}
})
})

describe('add', function(){
it('1000 random case', function(){
for (var i = 0; i < 1000; i++) {
var x = Math.random();
var y = Math.random();
assert.equal(x+y, cal.add(x, y));
}
})
})

describe('substract', function(){
it('0~9 - 0~9', function(){
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
assert.equal(i-j, cal.substract(i, j));
}
}
})
})

describe('substract', function(){
it('1000 random case', function(){
for (var i = 0; i < 1000; i++) {
var x = Math.random();
var y = Math.random();
assert.equal(x-y, cal.substract(x, y));
}
})
})

describe('multiple', function(){
it('0~9 * 0~9', function(){
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
assert.equal(i*j, cal.multiple(i, j));
}
}
})
})

describe('multiple', function(){
it('1000 random case', function(){
for (var i = 0; i < 1000; i++) {
var x = Math.random();
var y = Math.random();
assert.equal(x*y, cal.multiple(x, y));
}
})
})

describe('division', function(){
it('1~9 / 1~9', function(){
for (var i = 1; i < 10; i++) {
for (var j = 1; j < 10; j++) {
assert.equal(i/j, cal.division(i, j));
}
}
})
})

describe('division', function(){
it('1000 random case', function(){
for (var i = 0; i < 1000; i++) {
var x = Math.random();
var y = Math.random();
assert.equal(x/y, cal.division(x, y));
}
})
})

describe('Is number?', function(){
it('Positive', function(){
assert.equal(true, cal.isNumber('123.456'));
assert.equal(true, cal.isNumber('-789.012'));
assert.equal(true, cal.isNumber('345'));
assert.equal(true, cal.isNumber('-678'));
assert.equal(true, cal.isNumber('0.901'));
assert.equal(true, cal.isNumber('-0.234'));
assert.equal(true, cal.isNumber('0567'));
assert.equal(true, cal.isNumber('-0890'));
})
})

describe('Is number?', function(){
it('Negative', function(){
assert.equal(false, cal.isNumber('.12'));
assert.equal(false, cal.isNumber('+123.456'));
assert.equal(false, cal.isNumber('0.'));
assert.equal(false, cal.isNumber('123.456.789'));
assert.equal(false, cal.isNumber('1e10'));
assert.equal(false, cal.isNumber('1e-10'));
assert.equal(false, cal.isNumber('-1e10'));
assert.equal(false, cal.isNumber('-1e-10'));
assert.equal(false, cal.isNumber('0xabcdef'));
})
})
})