Skip to content

Implement calculator#1

Open
NedReidSoftwire wants to merge 2 commits intomasterfrom
implement-calculator
Open

Implement calculator#1
NedReidSoftwire wants to merge 2 commits intomasterfrom
implement-calculator

Conversation

@NedReidSoftwire
Copy link
Copy Markdown
Owner

No description provided.

Copy link
Copy Markdown
Owner Author

@NedReidSoftwire NedReidSoftwire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely amazing work! Just have some comments but it should be ready to merge after! 🤩

Comment thread calculator.js
console.log("Invalid operator " + operator);
}
// console.log('index: ' + index);
// console.log('current answer: ' + answer);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove these two comments please! 😊

Comment thread calculator.js
@@ -0,0 +1,47 @@
const operators = ['+', '-', '*', '/'];

exports.create_calculator = function (operator) {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently create_calculator is a function which generates an object with functions. Would it be better as a class instead? For example:

export class Calculator {
  constructor(operator) {
    this.operator = operator;
  }
add_number() {...}
calculate() {...}
}

This would make it slightly less messy! 😃

Comment thread calculator.js
answer = answer / number;
break;
default:
console.log("Invalid operator " + operator);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we throw this error when creating the calculator object, rather than during execution of the calculation. This way any errors regarding the operator will be thrown when the calculator is created, so it should be easier to spot when an invalid operator is used! ☺️

One way to do this would be to add in the constructor:

if (!operators.includes(operator)) {
  // throw an error here
}

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a related note, should this be throwing rather than logging? We should be consistent with the above error for when there are no numbers to calculate 🙂

Comment thread index.js
console.log('Enter the operator:');
const operator = readline.prompt();

console.log(`How many numbers do you want to ${operator} ?`);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will read as "How many numbers do we want to - "? Is this how we want to format the question? ☺️

Comment thread index.js
calculator.add_number(n);
}

console.log('The answer is: ' + calculator.calculate());
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think changing this to a template string would be really cool and fun and consistent with the log on line 9! 😎

Comment thread test/calculator_test.js
const calc = require('../calculator')

describe('calculator', function () {
describe('#calculate()', function () {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why there's a hash here? Just wanted to double check that this was the standard formatting for this repo! 😃

Comment thread test/calculator_test.js
const chai = require('chai');
const calc = require('../calculator')

describe('calculator', function () {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are an amazing start! 😍 Would it be worth having some tests for the other operators? 😀

Comment thread calculator.js
numbers.push(number);
},
calculate: function () {
// Return zero if there are no numbers to calculate
Copy link
Copy Markdown
Owner Author

@NedReidSoftwire NedReidSoftwire Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment necessary? 😀 It contradicts what's actually going on below!

Comment thread index.js
const operator = readline.prompt();

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we have a slightly more clear variable name here? Possibly something like "numberOfNumbersToOperateOn" or something slightly more concise and clear than that if you can think of one! 😅

Comment thread index.js

for (var i = 0; i < c; i++) {
console.log('Enter a number:');
const n = parseInt(readline.prompt());
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here in regards to the variable names! Possibly inputtedNumber or something like that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants