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..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 @@ -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. @@ -33,5 +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"); + +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 970cb9b641..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 @@ -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,29 @@ 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); 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..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,8 +21,25 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. +const validSuits = ["♠", "♣", "♥", "♦"]; + function getCardValue(card) { - // TODO: Implement this function + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card"); + } + + const suit = card.slice(-1); + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + + const rank = card.slice(0, -1); + if (rank === "A") return 11; + if (rank === "J" || rank === "Q" || rank === "K") return 10; + + 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. @@ -39,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"); @@ -52,3 +75,6 @@ try { } // What other invalid card cases can you think of? +// 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 -