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
17 changes: 17 additions & 0 deletions calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,22 @@ function substract(num1, num2){
return num1 - num2;
}

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

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

function random(){
return Math.floor((Math.random() * 100) + 1);
}

// exports method for other modules or files to use
module.exports.add = add;
module.exports.isNumber = isNumber;
module.exports.substract = substract;
module.exports.multiply = multiply;
module.exports.devide = devide;
module.exports.random = random;
26 changes: 16 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@
</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>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
<input type="text" id="ans" value="2" />
<button id="go_btn">Go!</button>

<section>
<input type="text" name="" id="num1" value="1" />
<select id="operator" size="1">
<option value="add">+</option>
<option value="substract">-</option>
<option value="multiply">*</option>
<option value="devide">÷</option>
</select>
<input type="text" id="num2" value="1" />
<div>=</div>
<input type="text" id="ans" value="2" />
<button id="go_btn">Go!</button>
</section>
<section>
<button id="random_btn">Random number</button>
</section>
<script src="calculator.js" type="text/javascript" charset="utf-8"></script>
<script src="main.js" type="text/javascript" charset="utf-8"></script>
</body>
Expand Down
12 changes: 12 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
window.onload = function(){

document.getElementById('random_btn').onclick = function(){

document.getElementById('num1').value = random();
}

document.getElementById('go_btn').onclick = function(){
var num1Str = document.getElementById('num1').value;
var num2Str = document.getElementById('num2').value;
Expand All @@ -16,6 +22,12 @@ window.onload = function(){
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 == "devide"){
document.getElementById('ans').value = devide(num1, num2);
}
else {
alert("Bad operator!")
}
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"dependencies": {
"mocha": "~2.3.2"
},
"repository":{}
"repository": {},
"devDependencies": {
"sinon": "^1.17.1"
}
}
60 changes: 55 additions & 5 deletions test/test_calculator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@
// to use methods from other files we simply use `require` with path name
var assert = require('assert');
var cal = require('../calculator.js')
var sinon = require('sinon')

describe('Calculator', function(){
describe('add', function(){
it('add', function(){
assert.equal('2', cal.add(1, 1));
describe('add', function(){
it('add', function(){
assert.equal('2', cal.add(1, 1));
})
})

describe('substract' , function(){
it('substract', function(){
assert.equal('3', cal.substract(5, 2));
})
})
/* ------------ Add new feature (× / ÷) ------------ */
describe('multiply', function(){
it('multiply', function(){
assert.equal('20', cal.multiply(4, 5));
})
})

describe('devide', function(){
it('devide', function(){
assert.equal('4', cal.devide(28, 7));
})
})
/* ----------------- negative path ----------------- */
describe('negativeAdd' ,function(){
it('negativeAdd', function(){
assert.notEqual('6', cal.add(2,3));
})
})
describe('negativeSubstract' , function(){
it('negativeSubstract', function(){
assert.notEqual('3', cal.substract(5, 1));
})
})

describe('negativeMultiply', function(){
it('negativeMultiply', function(){
assert.notEqual('21', cal.multiply(4, 5));
})
})

describe('devide', function(){
it('devide', function(){
assert.notEqual('5', cal.devide(28, 7));
})
})
/* --------- Add and mock the random number --------- */
describe('random', function() {
it('random', function() {
sinon.stub(Math, 'random').returns(20);
assert.equal('22', cal.add(Math.random(), 2));
})
})
})
})
})