From c0f5e98958362a875f0a4e69800cda97a75389f0 Mon Sep 17 00:00:00 2001 From: MacDonald91 Date: Wed, 4 Mar 2026 12:27:17 +0000 Subject: [PATCH] Sprint 3 implement functions and tests --- .../implement/1-get-angle-type.js | 40 +++++++++++++++ .../implement/2-is-proper-fraction.js | 30 ++++++++++++ .../implement/3-get-card-value.js | 49 ++++++++++++++++++- 3 files changed, 118 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 9e05a871e2..cb26378c26 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,20 @@ 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 +49,29 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +// Acute angle test +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +// Obtuse angle test +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); + +// Straight angle test +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +// Reflex angle test +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +// Boundary tests +assertEquals(getAngleType(1), "Acute angle"); +assertEquals(getAngleType(359), "Reflex angle"); + +// Invalid angle tests +assertEquals(getAngleType(0), "Invalid angle"); +assertEquals(getAngleType(360), "Invalid angle"); +assertEquals(getAngleType(-10), "Invalid angle"); +assertEquals(getAngleType(500), "Invalid angle"); \ No newline at end of file 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..6ddba8c47c 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 @@ -12,6 +12,16 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + + if (denominator === 0) { + return false; + } + + if (Math.abs(numerator) < Math.abs(denominator)) { + return true; + } + + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +41,23 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// numerator smaller than denominator +assertEquals(isProperFraction(3, 4), true); + +// numerator equal to denominator (not proper) +assertEquals(isProperFraction(5, 5), false); + +// numerator larger than denominator +assertEquals(isProperFraction(7, 3), false); + +// numerator is zero +assertEquals(isProperFraction(0, 5), true); + +// denominator is zero (invalid) +assertEquals(isProperFraction(1, 0), false); + +// negative numbers +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(-3, -2), false); \ No newline at end of file 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 c7559e787e..83708952f8 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 @@ -23,6 +23,30 @@ function getCardValue(card) { // TODO: Implement this function + + const suits = ["♠", "♥", "♦", "♣"]; + + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card"); + } + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + if (!suits.includes(suit)) { + throw new Error("Invalid card"); + } + + if (rank === "A") return 11; + if (rank === "J" || rank === "Q" || rank === "K") return 10; + + const numberValue = Number(rank); + + if (numberValue >= 2 && numberValue <= 10) { + return numberValue; + } + + throw new Error("Invalid card"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -41,6 +65,18 @@ function assertEquals(actualOutput, targetOutput) { // Examples: assertEquals(getCardValue("9♠"), 9); +// Ace +assertEquals(getCardValue("A♠"), 11); + +// Face cards +assertEquals(getCardValue("J♥"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♣"), 10); + +// Number cards +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("10♥"), 10); + // Handling invalid cards try { getCardValue("invalid"); @@ -49,4 +85,15 @@ try { console.error("Error was not thrown for invalid card"); } catch (e) {} -// What other invalid card cases can you think of? +// More invalid cases +try { + getCardValue("11♠"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("A"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +// What other invalid card cases can you think of? \ No newline at end of file