From 19bace25ef4b5e7b8ee9a745cfe9a9a0102ccad4 Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Tue, 2 Jun 2026 13:20:32 +0100 Subject: [PATCH 1/4] added assert tests for getAngleType --- .../implement/1-get-angle-type.js | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) 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 9e05a871e2..00bc42b05f 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 @@ -16,6 +16,19 @@ 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"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +48,15 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right 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 invalid = getAngleType(400); +assertEquals(invalid, "Invalid angle"); From 182860e470e86d9586514bf13fd47bc6236b5769 Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Tue, 2 Jun 2026 13:35:02 +0100 Subject: [PATCH 2/4] added assert tests for isProperFraction --- .../implement/2-is-proper-fraction.js | 30 ++++++++++++++++++- 1 file changed, 29 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 970cb9b641..bd86246c2c 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,7 +11,7 @@ // 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. @@ -31,3 +31,31 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// Basic non-proper examples +assertEquals(isProperFraction(2, 1), false); + +// Negative numerators or denominators should still use absolute values +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(-1, -2), true); + +// Zero numerator is a proper fraction when denominator != 0 +assertEquals(isProperFraction(0, 5), true); + +// Equal magnitude (including signs) is not proper +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(-5, 5), false); + +// Denominator zero (no division here, but should be considered invalid/proper=false) +assertEquals(isProperFraction(0, 0), false); +assertEquals(isProperFraction(3, 0), false); + +// Larger values and decimals +assertEquals(isProperFraction(100, 101), true); +assertEquals(isProperFraction(101, 100), false); +assertEquals(isProperFraction(0.5, 1), true); +assertEquals(isProperFraction(0.9999, 1), true); +assertEquals(isProperFraction(1, 1.0001), true); + +console.log("Completed tests in 2-is-proper-fraction.js"); From e836149208a572b6d659e818297c76d1f65fef42 Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Tue, 2 Jun 2026 15:38:39 +0100 Subject: [PATCH 3/4] added assert tests for getCardValue --- .../implement/3-get-card-value.js | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) 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 ff5c532e1d..43bf500914 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,41 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + // Student-friendly step-by-step parsing and validation. + // 1) Expect a string like "A♠", "10♥", "K♦" where the last character is the suit emoji + // 2) The rank is everything before the final character (so "10" or "A") + // 3) Check the suit is one of the four allowed suits, and the rank is valid + + if (typeof card !== "string") { + throw new Error('Card must be a string, e.g. "A♠"'); + } + + const text = card.trim(); + if (text.length < 2) { + throw new Error( + 'Invalid card: too short. Expect a rank and a suit, e.g. "10♠"' + ); + } + + // The suit is the last character, the rank is the rest + const suit = text[text.length - 1]; + const rank = text.slice(0, -1); + + const validSuits = new Set(["♠", "♥", "♦", "♣"]); + if (!validSuits.has(suit)) { + throw new Error(`Invalid suit "${suit}". Use one of: ♠ ♥ ♦ ♣`); + } + + // Handle special ranks first + if (rank === "A") return 11; + if (rank === "J" || rank === "Q" || rank === "K") return 10; + + // Otherwise expect a number between 2 and 10 + const n = Number(rank); + if (Number.isInteger(n) && n >= 2 && n <= 10) return n; + + // If we get here, the rank wasn't recognised + throw new Error(`Invalid rank "${rank}". Use A, 2-10, J, Q or K`); } // The line below allows us to load the getCardValue function into tests in other files. @@ -52,3 +86,26 @@ try { } // What other invalid card cases can you think of? + +// === Student-friendly examples === +// These examples show how the function behaves. Run the file with node to see messages. +console.log("\nRunning student-friendly examples for getCardValue:"); + +// Good cards +console.log("A♠ =>", getCardValue("A♠"), "(expected 11)"); +console.log("K♦ =>", getCardValue("K♦"), "(expected 10)"); +console.log("10♥ =>", getCardValue("10♥"), "(expected 10)"); +console.log("3♣ =>", getCardValue("3♣"), "(expected 3)"); + +// Examples that should throw (wrapped in try/catch so the script keeps running) +const examples = ["invalid", "9x", "a♠", ""]; +examples.forEach((example) => { + try { + const v = getCardValue(example); + console.log(`${example} => ${v} (unexpected: should have thrown)`); + } catch (e) { + console.log(`${example} => throws: ${e.message}`); + } +}); + +console.log("\nCompleted student-friendly examples"); From 88b62a45a0f09f0706941b9555d7fb9e820ee13c Mon Sep 17 00:00:00 2001 From: Zadri Abdule Date: Tue, 2 Jun 2026 20:58:06 +0100 Subject: [PATCH 4/4] added Jest tests to rewrite-tests-with-jest exercises 1-3 --- .../implement/1-get-angle-type.js | 22 +++--- .../implement/2-is-proper-fraction.js | 2 - .../implement/3-get-card-value.js | 73 ++++++------------- .../1-get-angle-type.test.js | 26 +++++++ .../2-is-proper-fraction.test.js | 32 ++++++++ .../3-get-card-value.test.js | 26 ++++++- 6 files changed, 113 insertions(+), 68 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 00bc42b05f..b2eacc9df2 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 @@ -46,17 +46,13 @@ 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 obtuse = getAngleType(120); -assertEquals(obtuse, "Obtuse angle"); - -const straight = getAngleType(180); -assertEquals(straight, "Straight angle"); - -const reflex = getAngleType(270); -assertEquals(reflex, "Reflex angle"); - -const invalid = getAngleType(400); -assertEquals(invalid, "Invalid angle"); +assertEquals(getAngleType(90), "Right angle"); +assertEquals(getAngleType(45), "Acute angle"); +assertEquals(getAngleType(135), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(270), "Reflex angle"); +assertEquals(getAngleType(-10), "Invalid angle"); +assertEquals(getAngleType(360), "Invalid angle"); +assertEquals(getAngleType(359.999), "Reflex angle"); +assertEquals(getAngleType("90"), "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 bd86246c2c..84c63244e7 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 @@ -57,5 +57,3 @@ assertEquals(isProperFraction(101, 100), false); assertEquals(isProperFraction(0.5, 1), true); assertEquals(isProperFraction(0.9999, 1), true); assertEquals(isProperFraction(1, 1.0001), true); - -console.log("Completed tests in 2-is-proper-fraction.js"); 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 43bf500914..84393b62f4 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 @@ -21,42 +21,25 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. -function getCardValue(card) { - // Student-friendly step-by-step parsing and validation. - // 1) Expect a string like "A♠", "10♥", "K♦" where the last character is the suit emoji - // 2) The rank is everything before the final character (so "10" or "A") - // 3) Check the suit is one of the four allowed suits, and the rank is valid - - if (typeof card !== "string") { - throw new Error('Card must be a string, e.g. "A♠"'); - } +const validSuits = ["♠", "♣", "♥", "♦"]; - const text = card.trim(); - if (text.length < 2) { - throw new Error( - 'Invalid card: too short. Expect a rank and a suit, e.g. "10♠"' - ); +function getCardValue(card) { + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card"); } - // The suit is the last character, the rank is the rest - const suit = text[text.length - 1]; - const rank = text.slice(0, -1); - - const validSuits = new Set(["♠", "♥", "♦", "♣"]); - if (!validSuits.has(suit)) { - throw new Error(`Invalid suit "${suit}". Use one of: ♠ ♥ ♦ ♣`); + const suit = card.slice(-1); + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); } - // Handle special ranks first + const rank = card.slice(0, -1); if (rank === "A") return 11; if (rank === "J" || rank === "Q" || rank === "K") return 10; - // Otherwise expect a number between 2 and 10 - const n = Number(rank); - if (Number.isInteger(n) && n >= 2 && n <= 10) return n; - - // If we get here, the rank wasn't recognised - throw new Error(`Invalid rank "${rank}". Use A, 2-10, J, Q or K`); + const num = Number(rank); + if (Number.isInteger(num) && num >= 2 && num <= 10) return num; + throw new Error("Invalid card"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -73,8 +56,14 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("3♣"), 3); assertEquals(getCardValue("9♠"), 9); - +assertEquals(getCardValue("10♦"), 10); +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("A♥"), 11); // Handling invalid cards try { getCardValue("invalid"); @@ -86,26 +75,6 @@ try { } // What other invalid card cases can you think of? - -// === Student-friendly examples === -// These examples show how the function behaves. Run the file with node to see messages. -console.log("\nRunning student-friendly examples for getCardValue:"); - -// Good cards -console.log("A♠ =>", getCardValue("A♠"), "(expected 11)"); -console.log("K♦ =>", getCardValue("K♦"), "(expected 10)"); -console.log("10♥ =>", getCardValue("10♥"), "(expected 10)"); -console.log("3♣ =>", getCardValue("3♣"), "(expected 3)"); - -// Examples that should throw (wrapped in try/catch so the script keeps running) -const examples = ["invalid", "9x", "a♠", ""]; -examples.forEach((example) => { - try { - const v = getCardValue(example); - console.log(`${example} => ${v} (unexpected: should have thrown)`); - } catch (e) { - console.log(`${example} => throws: ${e.message}`); - } -}); - -console.log("\nCompleted student-friendly examples"); +// lowercase rank +// invalid suit +// non-string input 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 d777f348d3..fffe45f49f 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 === 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 === 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 invalid`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType("90")).toEqual("Invalid angle"); + expect(getAngleType(1000)).toEqual("Invalid angle"); + expect(getAngleType(9999.999)).toEqual("Invalid angle"); +}); 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 7f087b2ba1..8c3c53c776 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,36 @@ 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(0, 0)).toEqual(false); + expect(isProperFraction(-1, 0)).toEqual(false); +}); + +test(`should return true when numerator is zero and denominator is non-zero`, () => { + expect(isProperFraction(0, 1)).toEqual(true); + expect(isProperFraction(0, -1)).toEqual(true); + expect(isProperFraction(0, 100)).toEqual(true); + expect(isProperFraction(0, -100)).toEqual(true); +}); + +test(`should return true when absolute value of numerator is less than absolute value of denominator`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +test(`should return false when absolute value of numerator is greater than or equal to absolute value of denominator`, () => { + expect(isProperFraction(1, 1)).toEqual(false); + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(-1, 1)).toEqual(false); + expect(isProperFraction(1, -1)).toEqual(false); + expect(isProperFraction(-1, -1)).toEqual(false); +}); + +test(`floating point values should be compared using absolute values`, () => { + expect(isProperFraction(0.5, 1)).toEqual(true); + expect(isProperFraction(1, 0.5)).toEqual(false); + expect(isProperFraction(-0.5, 1)).toEqual(true); + expect(isProperFraction(0.5, -1)).toEqual(true); + expect(isProperFraction(-0.5, -1)).toEqual(true); }); 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 cf7f9dae2e..dcb03884ff 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 @@ -11,10 +11,34 @@ 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 the numeric 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); +}); + // Face Cards (J, Q, K) +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); +}); + // Invalid Cards +test(`Should throw an error for invalid cards`, () => { + expect(() => getCardValue("1♠")).toThrow("Invalid card"); + expect(() => getCardValue("A")).toThrow("Invalid card"); + expect(() => getCardValue("J")).toThrow("Invalid card"); + expect(() => getCardValue("♠")).toThrow("Invalid card"); + expect(() => getCardValue("")).toThrow("Invalid 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 -