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

Implement calculator #1

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
47 changes: 47 additions & 0 deletions calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const operators = ['+', '-', '*', '/'];

exports.create_calculator = function (operator) {
const numbers = [];

return {
add_number: function (number) {
numbers.push(number);
},
calculate: function () {
// Return zero if there are no numbers to calculate
if (numbers.length === 0) {
throw 'Cannot calculate a result because no numbers were given';
}

let answer = numbers[0];

let i = 0;
numbers.forEach(function (number) {
if (i === 0) {
i++;
return;
}

switch (operator) {
case '+':
answer = answer + number;
break;
case '-':
answer = answer - number;
break;
case '/':
answer = answer / number;
break;
default:
console.log("Invalid operator " + operator);
}
// console.log('index: ' + index);
// console.log('current answer: ' + answer);

i++;
});

return answer;
}
}
}
19 changes: 16 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
const readline = require('readline-sync');
const calc = require('./calculator');

console.log('Welcome to the calculator!');

console.log('Please enter some input:');
const response = readline.prompt();
console.log('You entered: ' + response);
console.log('Enter the operator:');
const operator = readline.prompt();

console.log(`How many numbers do you want to ${operator} ?`);
const c = parseInt(readline.prompt());

const calculator = calc.create_calculator(operator);

for (var i = 0; i < c; i++) {
console.log('Enter a number:');
const n = parseInt(readline.prompt());
calculator.add_number(n);
}

console.log('The answer is: ' + calculator.calculate());
44 changes: 44 additions & 0 deletions test/calculator_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const assert = require('assert');
const chai = require('chai');
const calc = require('../calculator')

describe('calculator', function () {
describe('#calculate()', function () {
it('adds one number', function () {
const calculator = calc.create_calculator('+');

calculator.add_number(16);

assert.equal(16, calculator.calculate());
});

it('adds two numbers', function () {
const calculator = calc.create_calculator('+');

calculator.add_number(5);
calculator.add_number(4);

assert.equal(9, calculator.calculate());
});

it('adds many numbers', function () {
const calculator = calc.create_calculator('+');

calculator.add_number(5);
calculator.add_number(4);
calculator.add_number(0);
calculator.add_number(-20);
calculator.add_number(1486);
calculator.add_number(-402);
calculator.add_number(4);

assert.equal(1077, calculator.calculate());
});

it('rejects the calculation when there are no numbers', function () {
const calculator = calc.create_calculator('+');

chai.expect(calculator.calculate).to.throw('Cannot calculate a result because no numbers were given');
});
});
});