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..e5c4d3fdaf 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,8 +16,25 @@ 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. // This will be useful in the "rewrite tests with jest" step. module.exports = getAngleType; @@ -35,3 +52,12 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); +assertEquals(getAngleType(45), "Acute angle") +assertEquals(getAngleType(120), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(270), "Reflex angle") +assertEquals(getAngleType(-10), "Invalid angle") +assertEquals(getAngleType(0), "Invalid angle"); +assertEquals(getAngleType(360), "Invalid angle"); +assertEquals(getAngleType(400), "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..b9701c50c3 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,10 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator <= 0) { + return false; + } + return numerator < denominator; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +35,24 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +// Proper fractions +assertEquals(isProperFraction(2, 3), true); +assertEquals(isProperFraction(3, 4), true); +assertEquals(isProperFraction(0, 5), true); + +// Improper fractions +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(7, 3), false); + +// Denominator zero +assertEquals(isProperFraction(5, 0), false); + +// Negative numerators +assertEquals(isProperFraction(-1, 2), true); + +// Negative denominators +assertEquals(isProperFraction(1, -2), false); + +// Both negative +assertEquals(isProperFraction(-3, -2), false); + 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..3b20fa1935 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,40 @@ function getCardValue(card) { // TODO: Implement this function + const validRanks = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; + const validSuits = ["♠", "♥", "♦", "♣"]; + + // Suit is always the last character + const suit = card.slice(-1); + + // Rank is everything before the suit + const rank = card.slice(0, -1); + + // Validate rank and suit + if (!validRanks.includes(rank) || !validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + + // Convert rank to value + if (rank === "A") return 11; + if (["J", "Q", "K"].includes(rank)) return 10; + + // Number card + return Number(rank); } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,6 +74,12 @@ 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("J♦"), 10); +assertEquals(getCardValue("Q♥"), 10); +assertEquals(getCardValue("K♠"), 10); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("10♦"), 10); // Handling invalid cards try { @@ -52,3 +92,30 @@ try { } // What other invalid card cases can you think of? +try { + getCardValue("1♠"); // invalid rank + console.error("Error was not thrown for invalid rank"); +} catch (e) { + console.log("Error thrown for invalid rank 🎉"); +} + +try { + getCardValue("A?"); // invalid suit + console.error("Error was not thrown for invalid suit"); +} catch (e) { + console.log("Error thrown for invalid suit 🎉"); +} + +try { + getCardValue("10"); // missing suit + console.error("Error was not thrown for missing suit"); +} catch (e) { + console.log("Error thrown for missing suit 🎉"); +} + +try { + getCardValue(""); // empty string + console.error("Error was not thrown for empty string"); +} catch (e) { + console.log("Error thrown for empty string 🎉"); +} 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..89782bd29c 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(120)).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" for angles outside valid range`, () => { + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(0)).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 7f087b2ba1..c1b21b43d7 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,37 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Denominator zero +test("should return false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toEqual(false); +}); + +// Proper fractions +test("should return true for proper fractions (numerator < denominator)", () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(2, 3)).toEqual(true); + expect(isProperFraction(3, 4)).toEqual(true); + expect(isProperFraction(0, 5)).toEqual(true); // zero numerator is allowed +}); + +// Improper fractions +test("should return false for improper fractions (numerator >= denominator)", () => { + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(7, 3)).toEqual(false); +}); + +// Negative numerators +test("should return true when numerator is negative and denominator is positive", () => { + expect(isProperFraction(-1, 2)).toEqual(true); +}); + +// Negative denominators +test("should return false when denominator is negative", () => { + expect(isProperFraction(1, -2)).toEqual(false); +}); + +// Both negative +test("should return false when both numerator and denominator are negative", () => { + expect(isProperFraction(-3, -2)).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 cf7f9dae2e..812e620457 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,6 +7,9 @@ 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: @@ -14,6 +17,29 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +// Case 2: Number cards (2–10) +test("should return correct values for number cards", () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("7♦")).toEqual(7); + expect(getCardValue("10♥")).toEqual(10); +}); + +// Case 3: 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); +}); + +// Case 4: Invalid cards +test("should throw an error for invalid card values", () => { + expect(() => getCardValue("1♠")).toThrowError(); + expect(() => getCardValue("Z♦")).toThrowError(); + expect(() => getCardValue("")).toThrowError(); + expect(() => getCardValue("♠")).toThrowError(); +}); + + // 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