From 0ef19ecd0dbfa1b78a38e06e58a3d32a454d89ba Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Mon, 22 Jun 2026 13:05:35 +0100 Subject: [PATCH 1/2] written tests to check whether a given angle matches the expected output --- .../implement/1-get-angle-type.js | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 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 9e05a871e2..0110475245 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,13 +15,27 @@ // 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"; + } + 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) { @@ -33,5 +47,15 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); const right = getAngleType(90); assertEquals(right, "Right angle"); +const obtuse = getAngleType(135); +assertEquals(obtuse, "Obtuse angle"); +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); +const invalidAngle = getAngleType(-10); +assertEquals(invalidAngle, "Invalid angle"); From df94ea49dde79147e734a3340490d400fc72bad3 Mon Sep 17 00:00:00 2001 From: Yonatan Teklemariam Date: Sat, 27 Jun 2026 01:59:49 +0100 Subject: [PATCH 2/2] written jest test scripts for all the three functions --- .../implement/2-is-proper-fraction.js | 30 +++++++- .../implement/3-get-card-value.js | 70 ++++++++++++++++++- .../1-get-angle-type.test.js | 26 +++++++ .../2-is-proper-fraction.test.js | 35 +++++++++- .../3-get-card-value.test.js | 33 ++++++++- 5 files changed, 189 insertions(+), 5 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 970cb9b641..50858ea196 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,16 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (numerator === 0) { + return false; + } + if (denominator === 0) { + return false; + } + if (Number.isNaN(numerator) || Number.isNaN(denominator)) { + return false; + } + return Math.abs(numerator) < Math.abs(denominator); } // The line below allows us to load the isProperFraction function into tests in other files. @@ -25,6 +34,25 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } +// Proper fractions +assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(3, 4), true); +assertEquals(isProperFraction(-1, 5), true); +assertEquals(isProperFraction(1, -5), true); + +// Improper fractions +assertEquals(isProperFraction(5, 3), false); +assertEquals(isProperFraction(10, 10), false); +assertEquals(isProperFraction(-7, 3), false); + +// Zero cases +assertEquals(isProperFraction(0, 5), false); +assertEquals(isProperFraction(3, 0), false); + +// NaN cases +assertEquals(isProperFraction(NaN, 5), false); +assertEquals(isProperFraction(3, NaN), false); +assertEquals(isProperFraction(NaN, NaN), false); // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? 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..83d4459c36 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,34 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const ranks = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; + const suits = ["♠", "♥", "♦", "♣"]; + + let rank = card.slice(0, -1); // extracting the rank from the input + let suit = card.slice(-1); // extracting the suit from the input + if (!suits.includes(suit)) { + throw new Error("Invalid suit"); + } + if (!ranks.includes(rank)) { + throw new Error("Invalid rank"); + } + if (rank === "A") return 11; + if (["J", "Q", "K"].includes(rank)) return 10; + return Number(rank); } // The line below allows us to load the getCardValue function into tests in other files. @@ -39,7 +66,19 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: -assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("J♥"), 10); +// Number cards +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("9♥"), 9); +assertEquals(getCardValue("10♦"), 10); + +// Face cards +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♥"), 10); + +// Ace +assertEquals(getCardValue("A♠"), 11); // Handling invalid cards try { @@ -52,3 +91,30 @@ try { } // What other invalid card cases can you think of? +try { + getCardValue(""); + console.error("Error NOT thrown for empty string"); +} catch (e) { + console.log("Error thrown for empty string 🎉"); +} + +try { + getCardValue("1♠"); + console.error("Error NOT thrown for invalid rank"); +} catch (e) { + console.log("Error thrown for invalid rank 🎉"); +} + +try { + getCardValue("A"); + console.error("Error NOT thrown for missing suit"); +} catch (e) { + console.log("Error thrown for missing suit 🎉"); +} + +try { + getCardValue("A?"); + console.error("Error NOT thrown for invalid suit"); +} catch (e) { + console.log("Error thrown for invalid suit 🎉"); +} 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..92afb0d951 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(120)).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(250)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" for angles outside 0–360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); // boundary + expect(getAngleType(360)).toEqual("Invalid angle"); // boundary + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(400)).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..3c45c8642c 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 @@ -4,7 +4,40 @@ 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 +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +//Special case: numerator is zero +test(`should return false when numerator is zero`, () => { + expect(isProperFraction(0, 1)).toEqual(false); +}); +// Proper functions with absolute numerator < absolute denominator +test(`should return true for proper positive fractions`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 4)).toEqual(true); +}); +// Proper functions with negative numbers +test(`should return true for proper negative fractions`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -3)).toEqual(true); + expect(isProperFraction(-2, -5)).toEqual(true); +}); +// Improper functions with absolute numerator >= absolute denominator +test(`should return false for improper fractions`, () => { + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(10, 10)).toEqual(false); + expect(isProperFraction(-7, 3)).toEqual(false); +}); +// Special cases: numerator or denominator is NAN +test(`should return false when numerator or denominator is NaN`, () => { + expect(isProperFraction(NaN, 5)).toEqual(false); + expect(isProperFraction(3, NaN)).toEqual(false); + expect(isProperFraction(NaN, NaN)).toEqual(false); +}); +// Special cases: non-number strings +test(`should return false for non-numeric strings`, () => { + expect(isProperFraction("a", 5)).toEqual(false); + expect(isProperFraction(3, "b")).toEqual(false); +}); 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..5716aa3c83 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 @@ -9,6 +9,38 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +// Case 2: Number Cards (2-10) +test(`Should return correct values for number cards`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("7♥")).toEqual(7); + expect(getCardValue("9♦")).toEqual(9); + expect(getCardValue("10♣")).toEqual(10); +}); + +// Case 3: 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); +}); + +// Case 4: Invalid cards +test(`Should throw an error for invalid rank`, () => { + expect(() => getCardValue("1♠")).toThrowError("Invalid rank"); +}); + +test(`Should throw an error for invalid suit`, () => { + expect(() => getCardValue("A?")).toThrowError("Invalid suit"); +}); + +test(`Should throw an error for missing suit`, () => { + expect(() => getCardValue("A")).toThrowError(); +}); + +test(`Should throw an error for empty string`, () => { + expect(() => getCardValue("")).toThrowError(); +}); + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K) @@ -17,4 +49,3 @@ test(`Should return 11 when given an ace 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 -