Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,35 @@ 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 right angles", () => {
test("Classifies for exactly 90°", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
});
test("Classifies for angles greater than 0° and less than 90°", () => {
const acute = getAngleType(89);
assert.equal(acute, "Acute angle");
});
test("Classifies for angles greater than 90° and less than 180°", () => {
const obtuse = getAngleType(179);
assert.equal(obtuse, "Obtuse angle");
});
test("Classifies for exactly 180°", () => {
const straight = getAngleType(180);
assert.equal(straight, "Straight angle");
});
test("Classifies for angles greater than 180° and less than 360°", () => {
const reflex = getAngleType(359);
assert.equal(reflex, "Reflex angle");
});
test("Classifies for angles outside the valid range.", () => {
const invalid = getAngleType(361);
assert.equal(invalid, "Invalid angle");
});
Comment on lines +29 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be two boundary case for angles outside the valid range. Can you think of them? Please add them into this test suite. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added the lower boundary (exactly 0°) and upper boundary (exactly 360°). Thank you.

test("Classifies for exactly 0° (lower boundary)", () => {
const invalid = getAngleType(0);
assert.equal(invalid, "Invalid angle");
});
test("Classifies for exactly 360° (upper boundary)", () => {
const invalid = getAngleType(360);
assert.equal(invalid, "Invalid angle");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test cases should also include negatives and zeros. Please add them back. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Included negatives and zeros. Thank you.

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,51 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});
// improper fraction
test("numerator larger than denominator", () => {
assert.equal(isProperFraction(2, 1), false);
});
test("numerator equal to denominator", () => {
assert.equal(isProperFraction(1, 1), false);
});
// negative numerator
test("negative numerator, positive denominator, proper", () => {
assert.equal(isProperFraction(-1, 2), true);
});
test("negative numerator, positive denominator, improper", () => {
assert.equal(isProperFraction(-3, 2), false);
});
// negative denominator
test("positive numerator, negative denominator, proper", () => {
assert.equal(isProperFraction(1, -2), true);
});
test("positive numerator, negative denominator, improper", () => {
assert.equal(isProperFraction(3, -2), false);
});
// both negative
test("both negative, proper", () => {
assert.equal(isProperFraction(-1, -2), true);
});
test("both negative, improper", () => {
assert.equal(isProperFraction(-3, -2), false);
});
test("both negative, equal", () => {
assert.equal(isProperFraction(-2, -2), false);
});
// zero numerator
test("zero numerator, positive denominator", () => {
assert.equal(isProperFraction(0, 5), true);
});
test("zero numerator, negative denominator", () => {
assert.equal(isProperFraction(0, -5), true);
});
// zero denominator
test("zero denominator, positive numerator", () => {
assert.equal(isProperFraction(5, 0), false);
});
test("zero denominator, negative numerator", () => {
assert.equal(isProperFraction(-5, 0), false);
});
test("zero numerator and zero denominator", () => {
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,43 @@ test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
});

test("When the card is an ace (A), the function should return 11", () => {
assert.equal(getCardValue("A♠"), 11);
assert.equal(getCardValue("A♥"), 11);
assert.equal(getCardValue("A♦"), 11);
assert.equal(getCardValue("A♣"), 11);
});
test("When the card is a face card (J, Q, K), the function should return 10", () => {
assert.equal(getCardValue("J♠"), 10);
assert.equal(getCardValue("Q♥"), 10);
assert.equal(getCardValue("K♦"), 10);
});
test("When the card is a number card (2 to 10), the function should return its numeric 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");
assert.throws(
() => getCardValue("invalid"),
/Expected a number followed by a suit, but got "invalid"/,
"Expected clear error"
);
});

// TODO: What other invalid card cases can you think of?

test("Invalid card cases", () => {
assert.throws(() => getCardValue("1♠"));
assert.throws(() => getCardValue("11♠"));
assert.throws(() => getCardValue("X♠"));
assert.throws(() => getCardValue("A"));
assert.throws(() => getCardValue("2"));
assert.throws(() => getCardValue("2X"));
assert.throws(() => getCardValue(""));
});
13 changes: 12 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,16 @@
// 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";
}
return "Invalid angle";
}
5 changes: 5 additions & 0 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't think this program runs correctly for positives, negatives, and zeros. Can you fix it? Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added Math.abs to return absolute value of the number. Thank you.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else {
return false;
}
}
21 changes: 20 additions & 1 deletion implement-and-rewrite-tests/implement/3-get-card-value.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I am not quite sure about the correctness of this program. Please fix the following issues:

  1. formatting (e.g. indentation, brackets, etc)
  2. making if and else if cleaner to read, covering all possible combinations (instead of hardcode specific values)
  3. try to store card.slice(0, -1) into a variable and reuse it (instead of slice it multiple times)

If you find it difficult to approach this problem, try to book a mentored coding session and work this out with a volunteer together.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  1. I cleaned up the indentation and corrected the misplaced brackets in the number check.
  2. Cleaner if / else if: I removed the hardcoded cards and now check the rank against groups (ace, face cards, number cards), so every valid combination works.
  3. Reused slice: I stored card.slice(0, -1) in a rank variable (and card.slice(-1) in suit) so each is sliced once and reused.

Thank you.

@hackertainment hackertainment Oct 11, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good fix, and it looks much better now. Hope you would appreciate the importance of writing clean code :-)

Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,24 @@
// 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 validSuits = ["♠", "♥", "♦", "♣"];
const numberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
const faceRanks = ["J", "Q", "K"];

if (!validSuits.includes(suit)) {
throw new Error(`Expected a rank followed by a suit, but got "${card}"`);
}

if (rank === "A") {
return 11;
} else if (faceRanks.includes(rank)) {
return 10;
} else if (numberRanks.includes(rank)) {
return Number(rank);
}

throw new Error(`Expected a rank followed by a suit, but got "${card}"`);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good coverage of test cases. Well done!

Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@ import { getAngleType } from "../implement/1-get-angle-type.js";
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (angle > 90 && angle < 180)`, () => {
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(91)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (angle > 180 && angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return angles outside the valid range.)`, () => {
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There should be more test cases covering different combination of positive and negative values. Please add them back. Thank you.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added more test cases. Thank you

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,41 @@ 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.

// Special case: numerator is zero
test("should return true when numerator is zero and denominator is positive", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});
test(`should return true when numerator is zero and denominator is negative`, () => {
expect(isProperFraction(0, -1)).toEqual(true);
});
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test("should return false when denominator is 0 and numerator is negative", () => {
expect(isProperFraction(-1, 0)).toEqual(false);
});
// Special case: Positive numerator, negative denominator
test(`should return false when numerator and denominator are equal in size and denominator is negative`, () => {
expect(isProperFraction(1, -1)).toEqual(false);
});
test("should return true when numerator is positive, denominator is negative and numerator is smaller in size", () => {
expect(isProperFraction(1, -2)).toEqual(true);
});
// Special case: Both zero
test(`should return false when both denominator & numerator are zero`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
});
// Special case:Negative numerator, negative denominator
test(`should return true when both are negative and numerator is smaller in size`, () => {
expect(isProperFraction(-3, -6)).toEqual(true);
});
test("should return false when both are negative and numerator is larger in size", () => {
expect(isProperFraction(-6, -3)).toEqual(false);
});
// Special case:Negative numerator, positive denominator
test("should return true when numerator is negative, denominator is positive and numerator is smaller in size", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});
test("should return false when numerator is negative, denominator is positive and numerator is larger in size", () => {
expect(isProperFraction(-3, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,37 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`should return its numeric value When the card is a number card ("2" to "10")`, () => {
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);
});

// Face Cards (J, Q, K)

test("When the card is a face card (J, Q, K), the function should return 10", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Invalid Cards

test("Invalid card cases", () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("X♠")).toThrow();
expect(() => getCardValue("A")).toThrow();
expect(() => getCardValue("2")).toThrow();
expect(() => getCardValue("2X")).toThrow();
expect(() => getCardValue("")).toThrow();
});

// 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