-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
added calculator.js #2387
base: FabriceAB1
Are you sure you want to change the base?
added calculator.js #2387
Conversation
added calculator.js
Thank you for the submission, @FabriceAB1! I'll review your code shortly, hang tight. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great attempt, @FabriceAB1!
I would like to request a few changes before merging your work. Please review my comments below and make the appropriate changes to your code.
After you update your code locally, follow the instructions to save your changes locally and push your changes to your fork.
When you push your changes to your fork, I'll come back for another review.
There are 1 style guide violations in your contribution. I've marked them with inline comments for your convenience.
Please revisit your code and follow the style guide best practices.
Hint: You might be able to fix some issues automatically by running npm run lint -- --fix
All the tests are passing. Nice job!
Here is the regenerated code with all the information above:/**
* Checks if two values are numbers.
*
* @param {number} x The first number.
* @param {number} y The second number.
* @throws {TypeError} If either x or y is not a number.
*/
exports._check = (x, y) => {
if (typeof x !== 'number') {
throw new TypeError(`${x} is not a number`);
}
if (typeof y !== 'number') {
throw new TypeError(`${y} is not a number`);
}
};
/**
* Adds two numbers together.
*
* @param {number} x The first number.
* @param {number} y The second number.
* @return {number} The sum of x and y.
* @throws {TypeError} If either x or y is not a number.
*/
exports.add = (x, y) => {
exports._check(x, y);
return x + y;
};
/**
* Subtracts one number from another.
*
* @param {number} x The first number.
* @param {number} y The second number.
* @return {number} The difference of x and y.
* @throws {TypeError} If either x or y is not a number.
*/
exports.subtract = (x, y) => {
exports._check(x, y);
return x - y;
};
/**
* Multiplies two numbers together.
*
* @param {number} x The first number.
* @param {number} y The second number.
* @return {number} The product of x and y.
* @throws {TypeError} If either x or y is not a number.
*/
exports.multiply = (x, y) => {
exports._check(x, y);
return x * y;
};
/**
* Divides one number by another.
*
* @param {number} x The dividend.
* @param {number} y The divisor.
* @return {number} The quotient of x and y.
* @throws {TypeError} If either x or y is not a number.
* @throws {RangeError} If y is zero.
*/
exports.divide = (x, y) => {
exports._check(x, y);
if (y === 0) {
throw new RangeError('Cannot divide by zero');
}
return x / y;
};
module.exports = exports; This code includes:
|
Thanks for the changes, @FabriceAB1. I'm reviewing them now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great attempt, @FabriceAB1!
I would like to request a few changes before merging your work. Please review my comments below and make the appropriate changes to your code.
After you update your code locally, follow the instructions to save your changes locally and push your changes to your fork.
When you push your changes to your fork, I'll come back for another review.
There are 1 style guide violations in your contribution. I've marked them with inline comments for your convenience.
Please revisit your code and follow the style guide best practices.
Hint: You might be able to fix some issues automatically by running npm run lint -- --fix
All the tests are passing. Nice job!
To implement the
_check
function to make the tests pass, you can modify the code as follows:In this modified code:
_check
function checks if bothx
andy
are numbers. If either of them is not a number, it throws aTypeError
.add
,subtract
,multiply
, anddivide
functions call the_check
function at the beginning to check if the inputs are valid.divide
function, an additional check is added to prevent division by zero. Ify
is zero, it throws aRangeError
.With this implementation, the tests should pass, and the code should be more DRY (Don't Repeat Yourself) compliant.