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..a6d8d399a 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,19 @@ // 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 > 0 && angle < 90) { + return "Acute angle"; + } else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } else if (angle === 90) { + return "Right 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. @@ -34,4 +46,22 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles const right = getAngleType(90); +const acute1 = getAngleType(20); +const invalid = getAngleType(0); +const acute3 = getAngleType(89.999); +const obtuse = getAngleType(98.25); +const straightAngle = getAngleType(180); +const invalid1 = getAngleType(360); +const invalidAngle = getAngleType(362); +const reflexAngle2 = getAngleType(210); + assertEquals(right, "Right angle"); +assertEquals(acute1, "Acute angle"); +assertEquals(invalid, "Invalid angle"); +assertEquals(obtuse, "Obtuse angle"); +assertEquals(straightAngle, "Straight angle"); +assertEquals(invalid1, "Invalid angle"); +assertEquals(invalidAngle, "Invalid angle"); +assertEquals(reflexAngle2, "Reflex angle"); +assertEquals(acute3, "Acute 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..cb886e328 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 @@ -9,9 +9,16 @@ // Acceptance criteria: // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. - function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0) { + return false; + } + + if (Math.abs(numerator) >= Math.abs(denominator)) { + return false; + } + + return true; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +38,15 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(1, 10000), true); +assertEquals(isProperFraction(100, 2), false); +assertEquals(isProperFraction(1e2, 1e3), true); +assertEquals(isProperFraction(1e4, 1), false); +assertEquals(isProperFraction(-1, 1), false); +assertEquals(isProperFraction(1, -1), false); +assertEquals(isProperFraction(0, -1), true); +assertEquals(isProperFraction(-1, -2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, 0), false); +assertEquals(isProperFraction(-1, 0), 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 c7559e787..f2a4fa435 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 @@ -8,7 +8,7 @@ // "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" // The suit can be one of the following emojis: // "♠", "♥", "♦", "♣" -// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". +// For example: "A♠", "2♥", "10♥", "J"2♠"", "Q♦", "K♦". // When the card is an ace ("A"), the function should return 11. // When the card is a face card ("J", "Q", "K"), the function should return 10. @@ -22,7 +22,38 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + if (typeof card !== "string") { + throw new Error("Invalid string"); + } + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = [ + "A", + "J", + "Q", + "K", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + ]; + + if (!validSuits.includes(suit) || !validRanks.includes(rank)) { + throw new Error("Invalid string"); + } else if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + return Number(rank); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,13 +71,41 @@ 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("K♠"), 10); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("10♠"), 10); // Handling invalid cards -try { - getCardValue("invalid"); +function throwInvalidStringError(card) { + try { + getCardValue(card); + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); + } catch (e) { + assertEquals(e.message, "Invalid string"); + } +} - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card"); -} catch (e) {} +throwInvalidStringError("invalid"); +throwInvalidStringError(" ♠"); +throwInvalidStringError(" 6 ♠"); +throwInvalidStringError(" ♠"); +throwInvalidStringError(" **♠"); +throwInvalidStringError("10 **"); +throwInvalidStringError("A10♠"); +throwInvalidStringError("0x02♠"); +throwInvalidStringError("2.1♠"); +throwInvalidStringError("0002♠"); +throwInvalidStringError("22"); +throwInvalidStringError("♠2"); +throwInvalidStringError("2 ♠"); +throwInvalidStringError("2.♠"); +throwInvalidStringError("+2♠"); +throwInvalidStringError("♠A"); // What other invalid card cases can you think of? +// card with a mix of suit +//card with 0 at the front 01♠ 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..8074f3161 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,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right Angles" when(angle===90)`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles + +test(`should return "Obtuse angles" when (90< angle <180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); + expect(getAngleType(160)).toEqual("Obtuse angle"); + expect(getAngleType(100)).toEqual("Obtuse angle"); + expect(getAngleType(151.55)).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 (angle > 180 && angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(350)).toEqual("Reflex angle"); + expect(getAngleType(359.5)).toEqual("Reflex angle"); + expect(getAngleType(210)).toEqual("Reflex angle"); + expect(getAngleType(200)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`Should return "Invalid angle" when (0 ==360)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(500)).toEqual("Invalid angle"); + expect(getAngleType("1e5")).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 7f087b2ba..f8a2713c1 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,32 @@ 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`, () => { +test(`should return true when numerator is zero`, () => { + expect(isProperFraction(0, -4)).toEqual(true); + expect(isProperFraction(0, 1)).toEqual(true); +}); +test("should return false when denominator is zero", () => { + expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(10, 0)).toEqual(false); + expect(isProperFraction(-2, 0)).toEqual(false); + expect(isProperFraction(-1, 0)).toEqual(false); expect(isProperFraction(1, 0)).toEqual(false); }); + +test("should return false when denominator and numerator have both the same negative and positive integer", () => { + expect(isProperFraction(-1, 1)).toEqual(false); + expect(isProperFraction(1, -1)).toEqual(false); +}); + +test(`should return true when denominator is > numerator `, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(1, 10000)).toEqual(true); + + expect(isProperFraction(1e2, 1e3)).toEqual(true); + expect(isProperFraction(-1, 2)).toEqual(true); +}); + +test("should return false when denominator < numerator", () => { + expect(isProperFraction(1e4, 1)).toEqual(false); + expect(isProperFraction(100, 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 cf7f9dae2..1d6d0226b 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,48 @@ 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 Number from "2-10" when given a number card`, () => { + 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 when given a face card`, () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); + expect(getCardValue("Q♣")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); +}); // Invalid Cards +test("throws on Invalid String", () => { + expect(() => { + getCardValue("A10♠"); + }).toThrow("Invalid string"); + expect(() => { + getCardValue("A"); + }).toThrow("Invalid string"); + expect(() => { + getCardValue("110♠"); + }).toThrow("Invalid string"); + expect(() => { + getCardValue(" ♠"); + }).toThrow("Invalid string"); + expect(() => { + getCardValue("eeeeeej1234"); + }).toThrow("Invalid string"); +}); + // 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 -