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_check #2379

Merged
merged 3 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 8 additions & 22 deletions src/calculator.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,33 @@
exports._check = () => {
exports._check = (x, y) => {
// DRY up the codebase with this function
// First, move the duplicate error checking code here
// Then, invoke this function inside each of the others
// HINT: you can invoke this function with exports._check()
};

exports.add = (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`);
}
};

exports.add = (x, y) => {
exports._check(x, y);
return x + y;
};

exports.subtract = (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`);
}
exports._check(x, y);
return x - y;
};

exports.multiply = (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`);
}
exports._check(x, y);
return x * y;
};

exports.divide = (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`);
}
exports._check(x, y);
return x / y;
};

Expand Down
68 changes: 34 additions & 34 deletions src/calculator.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* eslint-disable no-unused-expressions */
const calculator = require('./calculator');
const calculator = require("./calculator");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)


describe.skip('_check', () => {
describe("_check", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

beforeEach(() => {
sinon.spy(calculator, '_check');
sinon.spy(calculator, "_check");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

});

afterEach(() => {
calculator._check.restore();
});

it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator._check(40, '2')).to.throw(TypeError);
it("should throw a TypeError if arguments are not numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator._check(40, "2")).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator._check(40, [])).to.throw(TypeError);
expect(() => calculator._check(40, {})).to.throw(TypeError);
expect(() => calculator._check('40', 2)).to.throw(TypeError);
expect(() => calculator._check("40", 2)).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator._check([], 2)).to.throw(TypeError);
expect(() => calculator._check({}, 2)).to.throw(TypeError);
});
Expand Down Expand Up @@ -44,94 +44,94 @@ describe.skip('_check', () => {
});
});

describe('add', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.add(40, '2')).to.throw(TypeError);
describe("add", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

it("should throw a TypeError if arguments are not numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.add(40, "2")).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.add(40, [])).to.throw(TypeError);
expect(() => calculator.add(40, {})).to.throw(TypeError);
expect(() => calculator.add('40', 2)).to.throw(TypeError);
expect(() => calculator.add("40", 2)).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.add([], 2)).to.throw(TypeError);
expect(() => calculator.add({}, 2)).to.throw(TypeError);
});

it('should add two positive numbers', () => {
it("should add two positive numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.add(40, 2)).to.equal(42);
});

it('should add two negative numbers', () => {
it("should add two negative numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.add(-40, -2)).to.equal(-42);
});

it('should add one positive number and one negative number', () => {
it("should add one positive number and one negative number", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.add(44, -2)).to.equal(42);
});
});

describe('subtract', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.subtract(40, '2')).to.throw(TypeError);
describe("subtract", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

it("should throw a TypeError if arguments are not numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.subtract(40, "2")).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.subtract(40, [])).to.throw(TypeError);
expect(() => calculator.subtract(40, {})).to.throw(TypeError);
expect(() => calculator.subtract('40', 2)).to.throw(TypeError);
expect(() => calculator.subtract("40", 2)).to.throw(TypeError);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(() => calculator.subtract([], 2)).to.throw(TypeError);
expect(() => calculator.subtract({}, 2)).to.throw(TypeError);
});

it('should subtract two positive numbers', () => {
it("should subtract two positive numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.subtract(44, 2)).to.equal(42);
});

it('should subtract two negative numbers', () => {
it("should subtract two negative numbers", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.subtract(-44, -2)).to.equal(-42);
});

it('should subtract one positive number and one negative number', () => {
it("should subtract one positive number and one negative number", () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Strings must use singlequote.
(learn more)

expect(calculator.subtract(40, -2)).to.equal(42);
});
});

describe('multiply', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.multiply(40, '2')).to.throw(TypeError);
describe("multiply", () => {
it("should throw a TypeError if arguments are not numbers", () => {
expect(() => calculator.multiply(40, "2")).to.throw(TypeError);
expect(() => calculator.multiply(40, [])).to.throw(TypeError);
expect(() => calculator.multiply(40, {})).to.throw(TypeError);
expect(() => calculator.multiply('40', 2)).to.throw(TypeError);
expect(() => calculator.multiply("40", 2)).to.throw(TypeError);
expect(() => calculator.multiply([], 2)).to.throw(TypeError);
expect(() => calculator.multiply({}, 2)).to.throw(TypeError);
});

it('should multiply two positive numbers', () => {
it("should multiply two positive numbers", () => {
expect(calculator.multiply(6, 7)).to.equal(42);
});

it('should multiply two negative numbers', () => {
it("should multiply two negative numbers", () => {
expect(calculator.multiply(-6, -7)).to.equal(42);
});

it('should multiply one positive number and one negative number', () => {
it("should multiply one positive number and one negative number", () => {
expect(calculator.multiply(6, -7)).to.equal(-42);
});
});

describe('divide', () => {
it('should throw a TypeError if arguments are not numbers', () => {
expect(() => calculator.divide(40, '2')).to.throw(TypeError);
describe("divide", () => {
it("should throw a TypeError if arguments are not numbers", () => {
expect(() => calculator.divide(40, "2")).to.throw(TypeError);
expect(() => calculator.divide(40, [])).to.throw(TypeError);
expect(() => calculator.divide(40, {})).to.throw(TypeError);
expect(() => calculator.divide('40', 2)).to.throw(TypeError);
expect(() => calculator.divide("40", 2)).to.throw(TypeError);
expect(() => calculator.divide([], 2)).to.throw(TypeError);
expect(() => calculator.divide({}, 2)).to.throw(TypeError);
});

it('should divide two positive numbers', () => {
it("should divide two positive numbers", () => {
expect(calculator.divide(84, 2)).to.equal(42);
});

it('should divide two negative numbers', () => {
it("should divide two negative numbers", () => {
expect(calculator.divide(-84, -2)).to.equal(42);
});

it('should divide one positive number and one negative number', () => {
it("should divide one positive number and one negative number", () => {
expect(calculator.divide(84, -2)).to.equal(-42);
});
});