Skip to content

Feat: IsEven and IsOdd #62

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

Merged
merged 1 commit into from
Oct 17, 2022
Merged
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
16 changes: 16 additions & 0 deletions Maths/IsEven.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @function IsEven
* @description Determine whether a number is even.
* @param {Number} num - A number.
* @return {Boolean} - Whether the given number is even.
* @see https://en.wikipedia.org/wiki/Parity_(mathematics)
* @example IsEven(1) = false
* @example IsEven(2) = true
*/
export const IsEven = (num: number): boolean => {
if (!Number.isInteger(num)) {
throw new Error("only integers can be even or odd");
}

return num % 2 === 0;
};
16 changes: 16 additions & 0 deletions Maths/IsOdd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @function IsOdd
* @description Determine whether a number is odd.
* @param {Number} num - A number.
* @return {Boolean} - Whether the given number is odd.
* @see https://en.wikipedia.org/wiki/Parity_(mathematics)
* @example IsOdd(1) = true
* @example IsOdd(2) = false
*/
export const IsOdd = (num: number): boolean => {
if (!Number.isInteger(num)) {
throw new Error("only integers can be even or odd");
}

return num % 2 !== 0;
};
16 changes: 16 additions & 0 deletions Maths/test/IsEven.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsEven } from "../IsEven";

describe("IsEven", () => {
test.each([[2, true], [1, false], [0, true], [-1, false], [-2, true]])(
"correct output for for %i",
(nums, expected) => {
expect(IsEven(nums)).toBe(expected);
},
);

test("only whole numbers should be accepted", () => {
expect(() => IsEven(0.5)).toThrowError(
"only integers can be even or odd",
);
});
});
16 changes: 16 additions & 0 deletions Maths/test/IsOdd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IsOdd } from "../IsOdd";

describe("IsOdd", () => {
test.each([[2, false], [1, true], [0, false], [-1, true], [-2, false]])(
"correct output for for %i",
(nums, expected) => {
expect(IsOdd(nums)).toBe(expected);
},
);

test("only whole numbers should be accepted", () => {
expect(() => IsOdd(0.5)).toThrowError(
"only integers can be even or odd",
);
});
});