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

102502559 #32

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
13 changes: 12 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\.])+$/;
return re.test(numStr);
}

Expand All @@ -11,5 +11,16 @@ 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;
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="multiply">x</option>
<option value="divide">/</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
Expand Down
25 changes: 16 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ 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);
}
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;
default:
alert("Bad operator!");
}
}
}
21 changes: 21 additions & 0 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,25 @@ describe('Calculator', function(){
assert.equal('2', cal.add(1, 1));
})
})
describe('substract', function(){
it('substract', function(){
assert.equal('3', cal.substract(4, 1));
})
})
describe('multiply', function(){
it('', function(){
assert.equal('2', cal.multiply(1, 2));
})
})
describe('divide', function(){
it('divide', function(){
assert.equal('2', cal.divide(2, 1));
})
})
describe('isNumber', function(){
it('isNumber', function(){
assert.equal('1', cal.isNumber(1));
assert.equal('0', cal.isNumber('YOLO'));
})
})
})