Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ import test from "node:test";

import { getAngleType } from "../implement/1-get-angle-type.js";

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
test("Classifies acute angles", () => {
assert.equal(getAngleType(45), "Acute angle");
});

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
});
test("Classifies obtuse angles", () => {
assert.equal(getAngleType(120), "Obtuse angle");
});

test("Classifies straight angles", () => {
assert.equal(getAngleType(180), "Straight angle");
});

test("Classifies reflex angles", () => {
assert.equal(getAngleType(270), "Reflex angle");
});

test("Invalid angles", () => {
assert.equal(getAngleType(0), "Invalid angle");
assert.equal(getAngleType(369), "Invalid angle");
assert.equal(getAngleType(-10), "Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,41 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("Basic proper fraction 2/5", () => {
assert.equal(isProperFraction(2, 5), true);
});

test("Basic proper fraction 3/5", () => {
assert.equal(isProperFraction(3, 5), true);
});

test("Checks equal numerator & denominator 5/5", () => {
assert.equal(isProperFraction(5, 5), false);
});

test("Checks numerator greater than denominator 7/5", () => {
assert.equal(isProperFraction(7, 5), false);
});

test("Checks negative numerator", () => {
assert.equal(isProperFraction(-2, 5), true);
});

test("Checks negative denominator", () => {
assert.equal(isProperFraction(2, -5), true);
});

test("Checks both numerator and denominator are negative", () => {
assert.equal(isProperFraction(-2, -5), true);
assert.equal(isProperFraction(-5, -2), false);
});

test("Checks zero numerator", () => {
assert.equal(isProperFraction(0, 5), true);
});

test("Checks zero denominator", () => {
assert.equal(isProperFraction(5, 0), false);
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,45 @@ import test from "node:test";

import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
test("Ace returns 11", () => {
assert.equal(getCardValue("A♠"), 11);
assert.equal(getCardValue("A♥"), 11);
assert.equal(getCardValue("A♦"), 11);
assert.equal(getCardValue("A♣"), 11);
});

test("Face cards return 10", () => {
assert.equal(getCardValue("J♣"), 10);
assert.equal(getCardValue("Q♦"), 10);
assert.equal(getCardValue("K♥"), 10);
});

test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
test("Number cards return their numerical value", () => {
assert.equal(getCardValue("2♥"), 2);
assert.equal(getCardValue("3♥"), 3);
assert.equal(getCardValue("4♥"), 4);
assert.equal(getCardValue("5♥"), 5);
assert.equal(getCardValue("6♥"), 6);
assert.equal(getCardValue("7♥"), 7);
assert.equal(getCardValue("8♥"), 8);
assert.equal(getCardValue("9♥"), 9);
assert.equal(getCardValue("10♥"), 10);
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
test("Invalid rank throws an error", () => {
assert.throws(() => getCardValue("X♠"), {
message: 'Expected a number followed by a suit, but got "X♠"',
});
});

// TODO: What other invalid card cases can you think of?
test("Invalid suit throws an error", () => {
assert.throws(() => getCardValue("9X"), {
message: 'Expected a number followed by a suit, but got "9X"',
});
});

test("Malformed input throws an error", () => {
assert.throws(() => getCardValue("invalid"), {
message: 'Expected a number followed by a suit, but got "invalid"',
});
});
14 changes: 13 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,17 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
} else {
return "Invalid angle";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator);
}
31 changes: 30 additions & 1 deletion implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,34 @@
// execute the code to ensure all tests pass.

export function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1);
const suit = card.slice(-1);

const validRanks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const validSuits = ["♠", "♥", "♦", "♣"];

if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}
if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
return Number(rank);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ import { getAngleType } from "../implement/1-get-angle-type.js";
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
test("Returns an acute angle between 0 and 90 degrees", () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

test("Returns a right angle at 90 degrees", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

test("Returns an obtuse angle between 90 and 180 degrees", () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

test("Returns a straight angle at 180 degrees", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

test("Returns a reflex angle between 180 and 360 degrees", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

test("Returns invalid angle outside the valid range", () => {
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
import { isProperFraction } from "../implement/2-is-proper-fraction.js";

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
test("should return true for proper fractions", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(2, 5)).toEqual(true);
});

test("should return false when numerator is not smaller", () => {
expect(isProperFraction(5, 5)).toEqual(false);
expect(isProperFraction(7, 5)).toEqual(false);
});

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});

test("should return true when numerator is zero", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test("should handle negative numerators", () => {
expect(isProperFraction(-2, 5)).toEqual(true);
expect(isProperFraction(-5, 2)).toEqual(false);
});

test("should handle negative denominators", () => {
expect(isProperFraction(2, -5)).toEqual(true);
expect(isProperFraction(5, -2)).toEqual(false);
});

test("should handle negative numerators and denominators", () => {
expect(isProperFraction(-2, -5)).toEqual(true);
expect(isProperFraction(-5, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests in Jest syntax to cover all possible outcomes.
test("Should return the correct value for number cards", () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("3♠")).toEqual(3);
expect(getCardValue("4♣")).toEqual(4);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("6♥")).toEqual(6);
expect(getCardValue("7♦")).toEqual(7);
expect(getCardValue("8♣")).toEqual(8);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♥")).toEqual(10);
});

test("Should return 10 for face cards", () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
test("Should return 11 for an ace of any suit", () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});

test("Should throw an error for an invalid rank", () => {
expect(() => getCardValue("X♠")).toThrow(
'Expected a number followed by a suit, but got "X♠"'
);
});

test("Should throw an error for an invalid suit", () => {
expect(() => getCardValue("9x")).toThrow(
'Expected a number followed by a suit, but got "9x"'
);
});

test("Should throw an error for malformed input", () => {
expect(() => getCardValue("invalid")).toThrow(
'Expected a number followed by a suit, but got "invalid"'
);
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -15,4 +51,3 @@ test(`Should return 11 when given an ace card`, () => {
// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

Loading