From 5a0dfa02c6c6f887a2521a277383df0341d48f50 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 11:01:02 +0000 Subject: [PATCH 1/9] write getAngleType function and assert tests --- .../implement/1-get-angle-type.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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..6724c70ff 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,7 +15,12 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle > 0 && angle < 90) return "Acute angle"; + if (angle === 90) return "Right angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + if (angle > 180 && angle < 360) return "Reflex angle"; + else return "Invalid angle"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +40,18 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const acute = getAngleType(47); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(139); +assertEquals(obtuse, "Obtuse angle"); + +const reflex = getAngleType(258); +assertEquals(reflex, "Reflex angle"); + +const invalid = getAngleType(360); +assertEquals(invalid, "Invalid angle"); From f5555b2b008a5d9c45ea51bf6cd19da4d568e21e Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 11:01:46 +0000 Subject: [PATCH 2/9] write jest tests for getAngleType function --- .../1-get-angle-type.test.js | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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..fc508f6f6 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 @@ -10,11 +10,42 @@ 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"); + expect(getAngleType(89.436)).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 (90 < angle < 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(92)).toEqual("Obtuse angle"); + expect(getAngleType(111.1)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle = 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(294)).toEqual("Reflex angle"); + expect(getAngleType(357.34)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle <= 0 || angle >= 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-90)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(411.65)).toEqual("Invalid angle"); + expect(getAngleType(256578)).toEqual("Invalid angle"); +}); From c85265757d6c08baec847a9f0e9802509b96e282 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 11:17:40 +0000 Subject: [PATCH 3/9] write a series of tests for isProperFraction() function --- .../implement/2-is-proper-fraction.js | 14 +++++++++ .../2-is-proper-fraction.test.js | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+) 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..19f66b5d8 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 @@ -31,3 +31,17 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +assertEquals(isProperFraction(9, 10), true); + +assertEquals(isProperFraction(0, 7), false); + +assertEquals(isProperFraction(5, 0), false); + +assertEquals(isProperFraction(3.5, 6), false); + +assertEquals(isProperFraction(2, 1), false); + +assertEquals(isProperFraction(9, 3), false); + +assertEquals(isProperFraction(4, 4.67), false); 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..3dd5bea94 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 @@ -5,6 +5,35 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // 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 < denominator and both are integers`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 9)).toEqual(true); + expect(isProperFraction(99, 100)).toEqual(true); +}); + +test(`should return false when denominator = zero`, () => { + expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(5, 0)).toEqual(false); +}); + +test(`should return false when numerator = zero`, () => { + expect(isProperFraction(0, 5)).toEqual(false); + expect(isProperFraction(0, 7)).toEqual(false); +}); + +test(`should return false when numerator > denominator`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(9, 3)).toEqual(false); + expect(isProperFraction(36, 7)).toEqual(false); +}); + +test(`should return false when either numerator or denominator or both are float numbers`, () => { + expect(isProperFraction(1.5, 2)).toEqual(false); + expect(isProperFraction(6, 7.1)).toEqual(false); + expect(isProperFraction(3.56, 2.4)).toEqual(false); +}); + test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); From ad5284449de82400b2e703d80e6dbe1b951222e6 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 12:17:42 +0000 Subject: [PATCH 4/9] rewrite all tests based on mathematical definition of proper fractions --- .../implement/2-is-proper-fraction.js | 18 ++++---- .../2-is-proper-fraction.test.js | 45 ++++++++++--------- 2 files changed, 33 insertions(+), 30 deletions(-) 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 19f66b5d8..0f4c4c72d 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 @@ -10,9 +10,7 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. -function isProperFraction(numerator, denominator) { - // TODO: Implement this function -} +function isProperFraction(numerator, 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. @@ -34,14 +32,18 @@ assertEquals(isProperFraction(1, 2), true); assertEquals(isProperFraction(9, 10), true); -assertEquals(isProperFraction(0, 7), false); +assertEquals(isProperFraction(-6, 8), true); + +assertEquals(isProperFraction(-2, -3), true); + +assertEquals(isProperFraction(3.5, 6), true); -assertEquals(isProperFraction(5, 0), false); +assertEquals(isProperFraction(4, 4.67), true); -assertEquals(isProperFraction(3.5, 6), false); +assertEquals(isProperFraction(7, 0), false); assertEquals(isProperFraction(2, 1), false); -assertEquals(isProperFraction(9, 3), false); +assertEquals(isProperFraction(-9, -3), false); -assertEquals(isProperFraction(4, 4.67), false); +assertEquals(isProperFraction(-5.68, 0), false); 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 3dd5bea94..c37a7b059 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 @@ -6,34 +6,35 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // Special case: numerator is zero -test(`should return true when numerator < denominator and both are integers`, () => { +test(`should return true when the absolute value of the numerator is less than the absolute value of the denominator and the denominator is not 0`, () => { expect(isProperFraction(1, 2)).toEqual(true); - expect(isProperFraction(3, 9)).toEqual(true); - expect(isProperFraction(99, 100)).toEqual(true); + expect(isProperFraction(3.5, 4)).toEqual(true); + expect(isProperFraction(-99, -100)).toEqual(true); + expect(isProperFraction(0, -5)).toEqual(true); + expect(isProperFraction(8, 12.5)).toEqual(true); + expect(isProperFraction(-6, 12)).toEqual(true); }); -test(`should return false when denominator = zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); - expect(isProperFraction(5, 0)).toEqual(false); +test(`should return false when the denominator is equal to zero`, () => { + expect(isProperFraction(-1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); + expect(isProperFraction(15, 0)).toEqual(false); + expect(isProperFraction(6.4, 0)).toEqual(false); + expect(isProperFraction(-8.5, 0)).toEqual(false); }); -test(`should return false when numerator = zero`, () => { - expect(isProperFraction(0, 5)).toEqual(false); - expect(isProperFraction(0, 7)).toEqual(false); -}); - -test(`should return false when numerator > denominator`, () => { +test(`should return false when the absolute value of the numerator is greater than the absolute value of the denominator`, () => { expect(isProperFraction(3, 2)).toEqual(false); - expect(isProperFraction(9, 3)).toEqual(false); - expect(isProperFraction(36, 7)).toEqual(false); -}); - -test(`should return false when either numerator or denominator or both are float numbers`, () => { - expect(isProperFraction(1.5, 2)).toEqual(false); - expect(isProperFraction(6, 7.1)).toEqual(false); - expect(isProperFraction(3.56, 2.4)).toEqual(false); + expect(isProperFraction(-9, 3)).toEqual(false); + expect(isProperFraction(-15, -4)).toEqual(false); + expect(isProperFraction(36, -5)).toEqual(false); + expect(isProperFraction(3.8, 2.7)).toEqual(false); }); -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); +test(`should return false when the absolute value of the numerator is equal to the absolute value of the denominator`, () => { + expect(isProperFraction(3, 3)).toEqual(false); + expect(isProperFraction(-19, -19)).toEqual(false); + expect(isProperFraction(7.45, 7.45)).toEqual(false); + expect(isProperFraction(67, -67)).toEqual(false); + expect(isProperFraction(-8, 8)).toEqual(false); }); From 8a750dd9d1ff4adf9693bfc46981320f94d729dc Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 12:50:52 +0000 Subject: [PATCH 5/9] write function is ProperFraction() to pass all tests --- .../implement/2-is-proper-fraction.js | 10 +++++++++- .../2-is-proper-fraction.test.js | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 0f4c4c72d..dd516b9e7 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 @@ -10,7 +10,15 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. -function isProperFraction(numerator, denominator) {} +function isProperFraction(numerator, denominator) { + // console.log(numerator, denominator); + // console.log(Math.abs(numerator), Math.abs(denominator)); + if (denominator !== 0 && Math.abs(numerator) < Math.abs(denominator)) { + return true; + } else { + return false; + } +} // 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. 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 c37a7b059..1d1091f50 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 @@ -8,11 +8,14 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return true when the absolute value of the numerator is less than the absolute value of the denominator and the denominator is not 0`, () => { expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(1, -3)).toEqual(true); expect(isProperFraction(3.5, 4)).toEqual(true); expect(isProperFraction(-99, -100)).toEqual(true); expect(isProperFraction(0, -5)).toEqual(true); + expect(isProperFraction(0, 56)).toEqual(true); expect(isProperFraction(8, 12.5)).toEqual(true); expect(isProperFraction(-6, 12)).toEqual(true); + expect(isProperFraction(0.03, 0.1)).toEqual(true); }); test(`should return false when the denominator is equal to zero`, () => { @@ -29,6 +32,7 @@ test(`should return false when the absolute value of the numerator is greater th expect(isProperFraction(-15, -4)).toEqual(false); expect(isProperFraction(36, -5)).toEqual(false); expect(isProperFraction(3.8, 2.7)).toEqual(false); + expect(isProperFraction(1, 0.7)).toEqual(false); }); test(`should return false when the absolute value of the numerator is equal to the absolute value of the denominator`, () => { From 9fd83a03c8ccba2a4fd4e18c6ced888243206a03 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Mon, 23 Feb 2026 13:05:47 +0000 Subject: [PATCH 6/9] refactor first test, splitting it into two, for more explicit testing about cases with numerator = 0 --- .../rewrite-tests-with-jest/2-is-proper-fraction.test.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 1d1091f50..46fa8c75f 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 @@ -11,13 +11,18 @@ test(`should return true when the absolute value of the numerator is less than t expect(isProperFraction(1, -3)).toEqual(true); expect(isProperFraction(3.5, 4)).toEqual(true); expect(isProperFraction(-99, -100)).toEqual(true); - expect(isProperFraction(0, -5)).toEqual(true); - expect(isProperFraction(0, 56)).toEqual(true); expect(isProperFraction(8, 12.5)).toEqual(true); expect(isProperFraction(-6, 12)).toEqual(true); expect(isProperFraction(0.03, 0.1)).toEqual(true); }); +test(`should return true when numerator is equal to 0 and denominator is either positive or negative`, () => { + expect(isProperFraction(0, -5)).toEqual(true); + expect(isProperFraction(0, 56)).toEqual(true); + expect(isProperFraction(0, 0.1)).toEqual(true); + expect(isProperFraction(0, -8.9)).toEqual(true); +}); + test(`should return false when the denominator is equal to zero`, () => { expect(isProperFraction(-1, 0)).toEqual(false); expect(isProperFraction(0, 0)).toEqual(false); From 9cc4680565c31ee8e5dddd421288ea3af8e62282 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 08:29:35 +0000 Subject: [PATCH 7/9] write tests for function getCardValue --- .../implement/3-get-card-value.js | 137 +++++++++++++++++- .../3-get-card-value.test.js | 56 ++++++- 2 files changed, 191 insertions(+), 2 deletions(-) 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..07b03b696 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 @@ -41,12 +41,147 @@ function assertEquals(actualOutput, targetOutput) { // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("4♣"), 4); + +assertEquals(getCardValue("A♥"), 11); + +assertEquals(getCardValue("J♦"), 10); + +assertEquals(getCardValue("Q♣"), 10); + +assertEquals(getCardValue("K♠"), 10); + +assertEquals(getCardValue("K♦"), 10); + +assertEquals(getCardValue("10♥"), 10); + +assertEquals(getCardValue("2♦"), 2); + // Handling invalid cards try { getCardValue("invalid"); // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); -} catch (e) {} +} catch (err) { + console.error(error); +} // What other invalid card cases can you think of? + +try { + getCardValue(""); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("1♥"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("11♠"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("0♦"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("4.8♣"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("G♦"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("9"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("K"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("9M"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("♠3"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue(6); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("A♦!"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} + +try { + getCardValue("♥"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(error); +} 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..4b4a3bef2 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,68 @@ 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); + expect(getCardValue("A♥")).toEqual(11); + expect(getCardValue("A♣")).toEqual(11); }); // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return the number value of a card when given an a number card`, () => { + expect(getCardValue("7♠")).toEqual(7); + expect(getCardValue("10♠")).toEqual(10); + expect(getCardValue("2♦")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("10♣")).toEqual(10); +}); + // Face Cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + expect(getCardValue("J♦")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); + expect(getCardValue("J♥")).toEqual(10); + expect(getCardValue("Q♣")).toEqual(10); +}); + // Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("invalid")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("11♠")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("0♦")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("4.8♣")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("G♦")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("9")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("K")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue(6)).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("♠3")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); + expect(() => getCardValue("9M")).toThrow( + "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." + ); +}); // 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 - From e2a6053086b619a5c16835ebef254b770b66551d Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 09:15:35 +0000 Subject: [PATCH 8/9] write getCardValue() function ensuring all tests pass --- .../implement/3-get-card-value.js | 72 +++++++++++++++---- .../3-get-card-value.test.js | 44 +++--------- 2 files changed, 68 insertions(+), 48 deletions(-) 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 07b03b696..a224fac58 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,7 +22,49 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const validSuits = ["♠", "♥", "♦", "♣"]; + if (typeof card !== "string") + throw new Error( + `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` + ); + + let cardSuit = card.slice(-1); + let cardRank = card.slice(0, -1); + + if (validSuits.find((suit) => suit === cardSuit)) { + console.log(cardSuit); + switch (cardRank) { + case "A": + console.log(cardRank, "ace rank"); + return 11; + + case "J": + case "Q": + case "K": + console.log(cardRank, "face rank"); + return 10; + + case "2": + case "3": + case "4": + case "5": + case "6": + case "7": + case "8": + case "9": + case "10": + console.log(cardRank, "number rank"); + return Number(cardRank); + + default: + throw new Error( + `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` + ); + } + } else + throw new Error( + `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` + ); } // The line below allows us to load the getCardValue function into tests in other files. @@ -64,7 +106,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } // What other invalid card cases can you think of? @@ -75,7 +117,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -84,7 +126,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -93,7 +135,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -102,7 +144,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -111,7 +153,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -120,7 +162,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -129,7 +171,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -138,7 +180,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -147,7 +189,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -156,7 +198,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -165,7 +207,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -174,7 +216,7 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } try { @@ -183,5 +225,5 @@ try { // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (err) { - console.error(error); + console.error(err); } 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 4b4a3bef2..3c57167d7 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 @@ -34,39 +34,17 @@ test(`Should return 10 when given a face card`, () => { // Invalid Cards test(`Should throw an error when given an invalid card`, () => { - expect(() => getCardValue("")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("invalid")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("11♠")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("0♦")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("4.8♣")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("G♦")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("9")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("K")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue(6)).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("♠3")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); - expect(() => getCardValue("9M")).toThrow( - "Invalid card format! Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣." - ); + expect(() => getCardValue("")).toThrow(/Invalid card format/); + expect(() => getCardValue("invalid")).toThrow(/Invalid card format/); + expect(() => getCardValue("11♠")).toThrow(/Invalid card format/); + expect(() => getCardValue("0♦")).toThrow(/Invalid card format/); + expect(() => getCardValue("4.8♣")).toThrow(/Invalid card format/); + expect(() => getCardValue("G♦")).toThrow(/Invalid card format/); + expect(() => getCardValue("9")).toThrow(/Invalid card format/); + expect(() => getCardValue("K")).toThrow(/Invalid card format/); + expect(() => getCardValue(6)).toThrow(/Invalid card format/); + expect(() => getCardValue("♠3")).toThrow(/Invalid card format/); + expect(() => getCardValue("9M")).toThrow(/Invalid card format/); }); // To learn how to test whether a function throws an error as expected in Jest, From 8adf134e8860ab5c098b3fc02ef6c0af1816a537 Mon Sep 17 00:00:00 2001 From: XiaoQuark Date: Tue, 24 Feb 2026 10:20:19 +0000 Subject: [PATCH 9/9] refactor code for more descriptive error messages --- .../implement/3-get-card-value.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) 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 a224fac58..231fd0a30 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 @@ -24,24 +24,19 @@ function getCardValue(card) { const validSuits = ["♠", "♥", "♦", "♣"]; if (typeof card !== "string") - throw new Error( - `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` - ); + throw new Error(`Invalid card format: "${card}". Expected a string.`); let cardSuit = card.slice(-1); let cardRank = card.slice(0, -1); - if (validSuits.find((suit) => suit === cardSuit)) { - console.log(cardSuit); + if (validSuits.includes(cardSuit)) { switch (cardRank) { case "A": - console.log(cardRank, "ace rank"); return 11; case "J": case "Q": case "K": - console.log(cardRank, "face rank"); return 10; case "2": @@ -53,17 +48,16 @@ function getCardValue(card) { case "8": case "9": case "10": - console.log(cardRank, "number rank"); return Number(cardRank); default: throw new Error( - `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` + `Invalid card format: "${card}" has an invalid rank "${cardRank}". Expected one of: A, 2–10, J, Q, K.` ); } } else throw new Error( - `Invalid card format: "${card}". Expected rank followed by suit, for example A♠, 10♥, K♦, or 7♣.` + `Invalid card format: "${card}" has an invalid suit "${cardSuit}". Expected one of: ♠, ♥, ♦, ♣.` ); }