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..8ed1b1de9 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,24 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + else if (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" + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -33,5 +50,17 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles +let invalid = getAngleType(-1); +assertEquals(invalid, "Invalid angle") +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") +invalid = getAngleType(360); +assertEquals(invalid, "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..a8dc8eaf9 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,9 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(3, 5), true); +assertEquals(isProperFraction(0, 5), true); +assertEquals(isProperFraction(4, 4), false); +assertEquals(isProperFraction(7, 3), false); +assertEquals(isProperFraction(-1, 2), false); +assertEquals(isProperFraction(4, 0), 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 c7559e787..4b6892605 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,22 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const rank = card.slice(0, -1); + const suit = card.slice(-1); + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + const validSuits = ["♠", "♥", "♦", "♣"]; + if (!validRanks.includes(rank) || !validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + if (rank === "A") { + return 11; + } + else if ( rank >= 2 && rank <= 10) { + return Number(rank); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,13 +55,17 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("10♥"), 10); // Handling invalid cards try { getCardValue("invalid"); + getCardValue("joker"); + getCardValue("1♦"); + 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 (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..375abcd8a 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 @@ -12,9 +12,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); - // Case 2: Right angle +test(`should return "Right angle" when (90)`, () => { + // Test various acute angles, including boundary cases + 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(179)).toEqual("Obtuse angle"); +}); // Case 4: Straight angle +test(`should return "Straight angle" when (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 out of range`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); +}); \ No newline at end of file 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..39b1cafd7 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,6 +4,22 @@ 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. +test("should return true for valid proper fractions", () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 10)).toEqual(true); + expect(isProperFraction(0, 5)).toEqual(true); +}); + +test("should return false when numerator is equal to or greater than denominator", () => { + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(10, 3)).toEqual(false); +}); + +test("should return false when numerator or denominator is negative", () => { + expect(isProperFraction(-1, 2)).toEqual(false); + expect(isProperFraction(1, -2)).toEqual(false); +}); + // Special case: numerator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).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 cf7f9dae2..d427affa4 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 @@ -14,6 +14,24 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +test("Should return the numeric value for cards 2 through 10", () => { + expect(getCardValue("2♥")).toBe(2); + expect(getCardValue("7♦")).toBe(7); + expect(getCardValue("10♣")).toBe(10); +}); + +test("should return 10 for all face cards (J, Q, K)", () => { + expect(getCardValue("J♠")).toBe(10); + expect(getCardValue("Q♥")).toBe(10); + expect(getCardValue("K♦")).toBe(10); +}); + +test("Should throw an error for invalid card strings", () => { + expect(() => getCardValue("1♠")).toThrow("Invalid card"); + expect(() => getCardValue("A-Z")).toThrow("Invalid card"); + expect(() => getCardValue("Joker")).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