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..7d49ed716 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,23 @@ // 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. @@ -35,3 +51,21 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, "Acute 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 invalid = getAngleType(-45); +assertEquals(invalid, "Invalid angle"); + +const invalid2 = getAngleType(361); +assertEquals(invalid2, "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..21ae23ba8 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,14 +11,20 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0) { + return false; + } + + return Math.abs(numerator) < Math.abs(denominator); } +module.exports = isProperFraction; + // The line below allows us to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. -module.exports = isProperFraction; +//module.exports = isProperFraction; -// Here's our helper again +/* Here's our helper again function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -30,4 +36,14 @@ function assertEquals(actualOutput, targetOutput) { // What combinations of numerators and denominators should you test? // Example: 1/2 is a proper fraction +/* assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(0, 1), true); +assertEquals(isProperFraction(-1, 2), false); +assertEquals(isProperFraction(1, -2), false); +assertEquals(isProperFraction(-1, -2), false); +assertEquals(isProperFraction(3, 4), true); +assertEquals(isProperFraction(4, 3), 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..64c568ece 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,32 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const validRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]; + const validSuits = ["♠","♥","♦","♣"]; + + // Check type + if (typeof card !== "string") { + throw new Error("Invalid card string"); + } + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + // Validate rank and suit + if (!validRanks.includes(rank) || !validSuits.includes(suit)) { + throw new Error("Invalid card string"); + } + + // Compute value + if (rank === "A") { + return 11; + } + + if (["J", "Q", "K"].includes(rank)) { + return 10; + } + + return parseInt(rank); } // The line below allows us to load the getCardValue function into tests in other files. @@ -39,7 +64,7 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: -assertEquals(getCardValue("9♠"), 9); +/* assertEquals(getCardValue("9♠"), 9); // Handling invalid cards try { @@ -50,3 +75,12 @@ try { } catch (e) {} // What other invalid card cases can you think of? +assertEquals(getCardValue("A"), 11); // Missing suit +try { + getCardValue("11♠"); // Invalid rank + console.error("Error was not thrown for invalid rank"); +} catch (e) {} + +try { getCardValue("9X"); // Invalid suit + console.error("Error was not thrown for invalid suit"); +} catch (e) {} */ 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..f86311c8d 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 @@ -6,7 +6,7 @@ const getAngleType = require("../implement/1-get-angle-type"); // including boundary and invalid cases. // Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { +test(`should return "Acute angle" for angles greater than 0° and less than 90°`, () => { // Test various acute angles, including boundary cases expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" for exactly 90°`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" for angles greater than 90° and less than 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" for exactly 180°`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" for angles greater than 180° and less than 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 angle ≤ 0° or angle ≥ 360°`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); // boundary case + expect(getAngleType(360)).toEqual("Invalid angle"); // boundary case + expect(getAngleType(361)).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..1d79431b1 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,38 @@ 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 -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); + +// Category 1: Numerator is zero +test("should return true when numerator is zero and denominator is positive", () => { + expect(isProperFraction(0, 1)).toBe(true); +}); +test("should return true when numerator is zero and denominator is negative", () => { + expect(isProperFraction(0, -1)).toBe(true); +}); + +// Category 2: Proper fractions (numerator < denominator) +test("should return true when numerator is less than denominator", () => { + expect(isProperFraction(1, 2)).toBe(true); + expect(isProperFraction(3, 5)).toBe(true); + expect(isProperFraction(-1, 2)).toBe(true); + expect(isProperFraction(1, -2)).toBe(true); +}); + +// Category 3: Improper fractions (numerator >= denominator) +test("should return false when abs(numerator) >= abs(denominator)", () => { + expect(isProperFraction(2, 1)).toBe(false); + expect(isProperFraction(5, 5)).toBe(false); + expect(isProperFraction(-3, 2)).toBe(false); + expect(isProperFraction(3, -2)).toBe(false); +}); + +// Category 4: Denominator is zero +test("should return false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toBe(false); }); + +// Category 5: Both numerator and denominator are zero + +test("should return false when numerator is zero and denominator is zero", () => { + expect(isProperFraction(0, 0)).toBe(false); +}); \ No newline at end of file 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..2b55d3d5d 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,29 @@ 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("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); +}); // Invalid Cards +test(`Should throw an error for invalid cards`, () => { + expect(() => getCardValue("invalid")).toThrow("Invalid card string"); + expect(() => getCardValue("A")).toThrow("Invalid card string"); // Missing suit + expect(() => getCardValue("11♠")).toThrow("Invalid card string"); // Invalid rank + expect(() => getCardValue("9X")).toThrow("Invalid card string"); // Invalid suit +}); // 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 + + + +