diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js index cf50f6cad..2876e31ee 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js @@ -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"); +}); +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"); +}); diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index a29084605..03b075ee8 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -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); +}); diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js index f5092d9b1..65dd92692 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js @@ -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("")); +}); diff --git a/implement-and-rewrite-tests/implement/1-get-angle-type.js b/implement-and-rewrite-tests/implement/1-get-angle-type.js index 8a6fba050..aab300e3e 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -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"; } diff --git a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js index f983800f0..965d3e04f 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -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; + } } diff --git a/implement-and-rewrite-tests/implement/3-get-card-value.js b/implement-and-rewrite-tests/implement/3-get-card-value.js index 9614ab1ee..9a510a4cc 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -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}"`); } diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index e0ba786ab..4d9a4b8f7 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -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"); +}); diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 5d49e2d8b..ef6323b4e 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -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); +}); diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index a6de5a82a..c7615ad48 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -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 -