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..d37f305526 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 @@ -1,5 +1,4 @@ // Implement a function getAngleType -// // When given an angle in degrees, it should return a string indicating the type of angle: // - "Acute angle" for angles greater than 0° and less than 90° // - "Right angle" for exactly 90° @@ -15,7 +14,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 "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 if(angle>0 && angle < 90){ + return "Acute angle"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -31,7 +47,9 @@ 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"); 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..1ef7e0c752 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,8 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(2, 2), false); + +assertEquals(isProperFraction(0, 2), true); +assertEquals(isProperFraction(-2, 1), 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..3be7c52c22 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 @@ -1,6 +1,4 @@ -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck - -// Implement a function getCardValue, when given a string representing a playing card, +/// Implement a function getCardValue, when given a string representing a playing card, // should return the numerical value of the card. // A valid card string will contain a rank followed by the suit. @@ -22,9 +20,22 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function -} + const rank = card.slice(0, -1); + + if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + + const value = Number(rank); + + if (value >= 2 && value <= 10) { + return value; + } + throw new Error("Invalid card!"); +} // The line below allows us to load the getCardValue function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getCardValue; @@ -39,7 +50,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 { @@ -51,4 +62,19 @@ try { console.log("Error thrown for invalid card 🎉"); } +try { + getCardValue("null"); + + console.error("Error was not thrown for null card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("0"); + + console.error("Error was not thrown for 0 card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} // 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 d777f348d3..e777e27b66 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,35 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle + +test(`Right angle`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles + +test(`Obtuse angle`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle + +test(`Straight angle`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles + +test("reflex angle", () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test("invalid angle", () => { + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(361)).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..0dbb1940d9 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 @@ -5,6 +5,29 @@ 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`, () => { + +// Case 1: Denominator is Zero (The Impossible Pizza) +test("should return false when denominator is zero", () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// Case 2: Numerator is Zero (Empty Plate) +test("should return true when numerator is zero and denominator is not zero", () => { + expect(isProperFraction(0, 2)).toEqual(true); +}); + +// Case 3: Normal Positive Fractions +test("should handle normal positive numbers correctly", () => { + expect(isProperFraction(1, 2)).toEqual(true); // Small top, big bottom -> True! + expect(isProperFraction(3, 2)).toEqual(false); // Big top, small bottom -> False! + expect(isProperFraction(2, 2)).toEqual(false); // Same size (whole pizza) -> False! +}); + +// Case 4: Negative Numbers (Minus Signs) +test("should ignore minus signs and look only at the size of the numbers", () => { + expect(isProperFraction(-1, 2)).toEqual(true); // 1 is smaller than 2 -> True! + expect(isProperFraction(1, -2)).toEqual(true); // 1 is smaller than 2 -> True! + expect(isProperFraction(-3, 2)).toEqual(false); // 3 is bigger than 2 -> False! + expect(isProperFraction(-2, -2)).toEqual(false); // Same size -> 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..1d6b734bd4 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,7 +14,27 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +test(`Number Cards (2-10)`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("5♠")).toEqual(5); + expect(getCardValue("10♠")).toEqual(10); +}); + +test(`Should return 11 when given an ace card`, () => { + expect(getCardValue("A♠")).toEqual(11); +}); + +test(`Should return 10 when given an face card`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); +}); + +test(`Should return 10 when given an face card`, () => { + expect(() => getCardValue("B♠")).toThrow("Invalid card!"); + expect(() => getCardValue("14♠")).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 -