From c40152a62d4e5282623e1940d139443c454f474f Mon Sep 17 00:00:00 2001 From: Divine Date: Fri, 6 Mar 2026 17:37:34 +0000 Subject: [PATCH 1/2] implement-and-rewrite-tests completed --- .../implement/1-get-angle-type.js | 43 +++++++++- .../implement/2-is-proper-fraction.js | 18 ++++- .../implement/3-get-card-value.js | 81 ++++++++++++++++++- .../1-get-angle-type.test.js | 26 ++++++ .../2-is-proper-fraction.test.js | 42 ++++++++++ 5 files changed, 204 insertions(+), 6 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..0845ebacd 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,19 @@ // execute the code to ensure all tests pass. 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 +47,32 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +// Test Acute angle +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +// Test Obtuse angle +const obtuse = getAngleType(135); +assertEquals(obtuse, "Obtuse angle"); + +// Test Straight angle +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +// Test Reflex angle +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +// Test Invalid angles +const zero = getAngleType(0); +assertEquals(zero, "Invalid angle"); + +const negative = getAngleType(-45); +assertEquals(negative, "Invalid angle"); + +const fullCircle = getAngleType(360); +assertEquals(fullCircle, "Invalid angle"); + +const over360 = getAngleType(400); +assertEquals(over360, "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..1ca792a9a 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 numerator > 0 && denominator > 0 && numerator < denominator; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +31,19 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// Test improper fractions +assertEquals(isProperFraction(3, 2), false); +assertEquals(isProperFraction(5, 5), false); + +// Test negative numbers +assertEquals(isProperFraction(-1, 2), false); +assertEquals(isProperFraction(1, -2), false); + +// Test zero +assertEquals(isProperFraction(0, 2), false); +assertEquals(isProperFraction(1, 0), false); + +// Test other proper fractions +assertEquals(isProperFraction(2, 3), true); +assertEquals(isProperFraction(7, 10), true); 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..e59ce41ce 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,39 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + if (typeof card !== 'string') { + throw new Error('Invalid card: must be a string'); + } + + const suits = ['♠', '♥', '♦', '♣']; + const ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; + + let rank, suit; + if (card.length === 2) { + rank = card[0]; + suit = card[1]; + } else if (card.length === 3 && card.startsWith('10')) { + rank = '10'; + suit = card[2]; + } else { + throw new Error('Invalid card format'); + } + + if (!suits.includes(suit)) { + throw new Error('Invalid suit'); + } + + if (!ranks.includes(rank)) { + throw new Error('Invalid rank'); + } + + if (rank === 'A') { + return 11; + } else if (['J', 'Q', 'K'].includes(rank)) { + return 10; + } else { + return parseInt(rank); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -41,12 +73,53 @@ function assertEquals(actualOutput, targetOutput) { // Examples: assertEquals(getCardValue("9♠"), 9); +// Test all ranks +assertEquals(getCardValue("A♠"), 11); +assertEquals(getCardValue("2♥"), 2); +assertEquals(getCardValue("3♦"), 3); +assertEquals(getCardValue("4♣"), 4); +assertEquals(getCardValue("5♠"), 5); +assertEquals(getCardValue("6♥"), 6); +assertEquals(getCardValue("7♦"), 7); +assertEquals(getCardValue("8♣"), 8); +assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("10♥"), 10); +assertEquals(getCardValue("J♦"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♠"), 10); + +// Test different suits +assertEquals(getCardValue("A♥"), 11); +assertEquals(getCardValue("K♦"), 10); + // 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 (e) { } + +// Test invalid rank +try { + getCardValue("1♠"); + console.error("Error was not thrown for invalid rank"); +} catch (e) { } + +// Test invalid suit +try { + getCardValue("A♤"); + console.error("Error was not thrown for invalid suit"); +} catch (e) { } + +// Test wrong length +try { + getCardValue("A"); + console.error("Error was not thrown for wrong length"); +} catch (e) { } + +// Test non-string +try { + getCardValue(123); + console.error("Error was not thrown for non-string"); +} catch (e) { } // What other invalid card cases can you think of? 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..f8e77404c 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" for angles <= 0 or >= 360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(360)).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 7f087b2ba..37876be69 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,3 +8,45 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Positive proper fractions +test(`should return true for positive proper fractions`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(2, 3)).toEqual(true); + expect(isProperFraction(7, 10)).toEqual(true); +}); + +// Positive improper fractions +test(`should return false for positive improper fractions`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(10, 3)).toEqual(false); +}); + +// Negative numerator +test(`should return false when numerator is negative`, () => { + expect(isProperFraction(-1, 2)).toEqual(false); + expect(isProperFraction(-3, 4)).toEqual(false); +}); + +// Negative denominator +test(`should return false when denominator is negative`, () => { + expect(isProperFraction(1, -2)).toEqual(false); + expect(isProperFraction(3, -4)).toEqual(false); +}); + +// Zero numerator +test(`should return false when numerator is zero`, () => { + expect(isProperFraction(0, 2)).toEqual(false); + expect(isProperFraction(0, -3)).toEqual(false); +}); + +// Both negative (improper) +test(`should return false for negative improper fractions`, () => { + expect(isProperFraction(-3, -2)).toEqual(false); +}); + +// Both negative (proper) +test(`should return false for negative proper fractions`, () => { + expect(isProperFraction(-1, -2)).toEqual(false); +}); From 508e48646b621c4ee186f320cc2fafdd8a608436 Mon Sep 17 00:00:00 2001 From: Divine Date: Fri, 6 Mar 2026 18:27:12 +0000 Subject: [PATCH 2/2] Finished 3-get-card-value-test --- .../3-get-card-value.test.js | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) 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..a677b2e9e 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,41 @@ 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) -// Face Cards (J, Q, K) -// Invalid Cards +// 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`, () => { + expect(getCardValue("J♥")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); + 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 +// Invalid Cards +test(`Should throw an error for invalid cards`, () => { + expect(() => getCardValue("invalid")).toThrow(); + expect(() => getCardValue("1♠")).toThrow(); + expect(() => getCardValue("A♤")).toThrow(); + expect(() => getCardValue("A")).toThrow(); + expect(() => getCardValue("")).toThrow(); + expect(() => getCardValue(123)).toThrow(); +});