function Calculator(num1, num2) {
this.num1 = num1;
this.num2 = num2;
this.add = function(num1 = this.num1, num2 = this.num2) {
if(isNaN(num1)) {
throw new TypeError(num1 + ' is not the valid number');
}
if(isNaN(num2)) {
throw new TypeError(num2 + ' is not the valid number');
}
return parseFloat(num1) + parseFloat(num2);
};
this.minus = function(num1 = this.num1, num2 = this.num2) {
if(isNaN(num1)) {
throw new TypeError(num1 + ' is not the valid number');
}
if(isNaN(num2)) {
throw new TypeError(num2 + ' is not the valid number');
}
return parseFloat(num1) - parseFloat(num2);
}
this.mul = function(num1 = this.num1, num2 = this.num2) {
if(isNaN(num1)) {
throw new TypeError(num1 + ' is not the valid number');
}
if(isNaN(num2)) {
throw new TypeError(num2 + ' is not the valid number');
}
return parseFloat(num1) * parseFloat(num2);
}
this.divide = function(num1 = this.num1, num2 = this.num2) {
if(isNaN(num1)) {
throw new TypeError(num1 + ' is not the valid number');
}
if(isNaN(num2)) {
throw new TypeError(num2 + ' is not the valid number');
}
num1 = parseFloat(num1);
num2 = parseFloat(num2);
if(num2 == 0) {
throw new Error(num2 + ' should not be the zero number');
}
return num1 / num2;
}
}
module.exports = Calculator;
function SimpleMath(num1, num2) {
this.num1 = num1;
this.num2 = num2;
this.pow = function(num1 = this.num1, num2 = this.num2) {
if(isNaN(num1)) {
throw new TypeError(num1 + ' is not the valid number');
}
if(isNaN(num2)) {
throw new TypeError(num2 + ' is not the valid number');
}
num1 = parseFloat(num1);
num2 = parseFloat(num2);
return Math.pow(num1, num2);
}
this.sqrt = function(num = this.num) {
if(isNaN(num)) {
throw new TypeError(num + ' is not the valid number');
}
num = parseFloat(num);
return Math.sqrt(num);
}
this.log = function(num = this.num1) {
if(isNaN(num)) {
throw new TypeError(num + ' is not the valid number');
}
num = parseFloat(num);
return Math.log(num);
}
this.logTen = function(num = this.num1) {
if(isNaN(num)) {
throw new TypeError(num + ' is not the valid number');
}
num = parseFloat(num);
return Math.log10(num);
}
}
module.exports = SimpleMath;
const assert = require('assert');
var calculatorjs = require('../src/main');
var Calculator = calculatorjs.Calculator;
var SimpleMath = calculatorjs.SimpleMath;
var calculator = new Calculator(1, 2);
var simpleMath = new SimpleMath(2, 3);
// assert.equal(expected, result);
assert.equal(3, calculator.add());
assert.equal(-1, calculator.minus());
assert.equal(2, calculator.mul());
assert.equal(0.5, calculator.divide());
assert.equal(8, simpleMath.pow());
assert.equal(2, simpleMath.sqrt(4));
assert.equal(0, simpleMath.log(1));
assert.equal(0, simpleMath.logTen(1));
// It's equivalent to assert.throws(function() {calculator.add('ff', 'ff');}, TypeError);
assert.throws(() => calculator.add('ff', 'ff'), TypeError);
assert.throws(() => calculator.minus('ff', 'ff'), TypeError);
assert.throws(() => calculator.mul('ff', 'ff'), TypeError);
assert.throws(() => calculator.divide('ff', 'ff'), TypeError);
assert.throws(() => calculator.divide(1, 0), Error);
sudo: false
language: node_js
node_js:
- 6
- 8
- "stable"
script: npm test
after_success:
- npm run-script coveralls-coverage
- npm run-script codecov-coverage