From 18b7aff4e55e24db290374274b700fd78c3dfeba Mon Sep 17 00:00:00 2001 From: Samual-Hu Date: Sat, 28 Feb 2026 10:14:24 +0000 Subject: [PATCH] Complete implement and Jest rewrite tests for angle, fraction, and cards --- .../implement/1-get-angle-type.js | 35 ++++++++++++--- .../implement/2-is-proper-fraction.js | 25 +++++++---- .../implement/3-get-card-value.js | 44 ++++++++++++++----- .../1-get-angle-type.test.js | 26 +++++++++++ .../2-is-proper-fraction.test.js | 24 ++++++++++ .../3-get-card-value.test.js | 32 +++++++++++--- 6 files changed, 153 insertions(+), 33 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 9e05a871e..d1e701879 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -15,15 +15,17 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle === 90) return "Right angle"; + if (angle === 180) return "Straight angle"; + if (angle > 0 && angle < 90) return "Acute angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle > 180 && angle < 360) return "Reflex angle"; + + return "Invalid angle"; } -// The line below allows us to load the getAngleType function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. module.exports = getAngleType; -// This helper function is written to make our assertions easier to read. -// If the actual output matches the target output, the test will pass function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -31,7 +33,26 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +const invalidZero = getAngleType(0); +assertEquals(invalidZero, "Invalid angle"); + +const invalidNegative = getAngleType(-45); +assertEquals(invalidNegative, "Invalid angle"); + +const invalidOver = getAngleType(361); +assertEquals(invalidOver, "Invalid angle"); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b64..fb5b740d3 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,14 +11,11 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return Math.abs(numerator) < Math.abs(denominator); } -// The line below allows us to load the isProperFraction function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. module.exports = isProperFraction; -// Here's our helper again function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -26,8 +23,20 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases. -// What combinations of numerators and denominators should you test? +const properFraction = isProperFraction(2, 3); +assertEquals(properFraction, true); -// Example: 1/2 is a proper fraction -assertEquals(isProperFraction(1, 2), true); +const improperFraction = isProperFraction(5, 2); +assertEquals(improperFraction, false); + +const negativeFraction = isProperFraction(-4, 7); +assertEquals(negativeFraction, true); + +const equalFraction = isProperFraction(3, 3); +assertEquals(equalFraction, false); + +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +const zeroDenominator = isProperFraction(5, 0); +assertEquals(zeroDenominator, false); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index c7559e787..d87dbb8a4 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -22,14 +22,26 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const rank = card.slice(0, -1); + const suit = card.slice(-1); + + const validSuits = ["♠", "♥", "♦", "♣"]; + const faceCards = ["10", "J", "Q", "K"]; + const numberCards = ["2", "3", "4", "5", "6", "7", "8", "9"]; + + if (!validSuits.includes(suit)) { + throw new Error("Invalid card suit"); + } + + if (rank === "A") return 11; + if (faceCards.includes(rank)) return 10; + if (numberCards.includes(rank)) return Number(rank); + + throw new Error("Invalid card rank"); } -// The line below allows us to load the getCardValue function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. module.exports = getCardValue; -// Helper functions to make our assertions easier to read. function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -37,16 +49,26 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("5♥"), 5); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("A♣"), 11); -// Handling invalid cards try { getCardValue("invalid"); + console.error("Error was not thrown for completely invalid card"); +} catch (error) {} - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card"); -} catch (e) {} +try { + getCardValue("1♠"); + console.error("Error was not thrown for invalid card rank"); +} catch (error) { + assertEquals(error.message, "Invalid card rank"); +} -// What other invalid card cases can you think of? +try { + getCardValue("5X"); + console.error("Error was not thrown for invalid card suit"); +} catch (error) { + assertEquals(error.message, "Invalid card suit"); +} \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index d777f348d..629b9243b 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when angle is 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is 180`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); +}); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 7f087b2ba..ae8ee1b19 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -7,4 +7,28 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // Special case: numerator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(-5, 0)).toEqual(false); }); + +test(`should return true for positive proper fractions`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(2, 5)).toEqual(true); +}); + +test(`should return false for positive improper fractions or equal values`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(5, 5)).toEqual(false); +}); + +test(`should return true when numerator is zero`, () => { + expect(isProperFraction(0, 5)).toEqual(true); + expect(isProperFraction(0, -5)).toEqual(true); +}); + +test(`should evaluate correctly with various negative number combinations`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(true); + expect(isProperFraction(-5, 2)).toEqual(false); + expect(isProperFraction(5, -2)).toEqual(false); +}); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index cf7f9dae2..9a23b27bc 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -7,14 +7,32 @@ const getCardValue = require("../implement/3-get-card-value"); // Case 1: Ace (A) test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♥")).toEqual(11); }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards +test(`Should return the correct numeric value for number cards`, () => { + expect(getCardValue("2♣")).toEqual(2); + expect(getCardValue("7♥")).toEqual(7); + expect(getCardValue("10♦")).toEqual(10); +}); + +test(`Should return 10 for face cards (J, Q, K)`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); +}); -// 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 +test(`Should throw an error for invalid card rank`, () => { + expect(() => getCardValue("1♠")).toThrow("Invalid card rank"); + expect(() => getCardValue("11♥")).toThrow("Invalid card rank"); + expect(() => getCardValue("Z♦")).toThrow("Invalid card rank"); + expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank"); + expect(() => getCardValue("3.1416♠")).toThrow("Invalid card rank"); +}); + +test(`Should throw an error for invalid card suit`, () => { + expect(() => getCardValue("5X")).toThrow("Invalid card suit"); + expect(() => getCardValue("10-")).toThrow("Invalid card suit"); + expect(() => getCardValue("A/")).toThrow("Invalid card suit"); +});