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..3a9dc661ce 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 < 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. @@ -31,7 +43,10 @@ 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(91), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(185), "Reflex angle"); +assertEquals(getAngleType(365), "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..342b3ba67a 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,14 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (isNaN(numerator) && isNaN(denominator)) { + return false; + } + + if (numerator < denominator) { + return true; + } + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -26,8 +33,11 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(3, 2), false); +assertEquals(isProperFraction(4312, 5000), true); +assertEquals(isProperFraction("Invalid", "data"), 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..fef9a016c7 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,48 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. +function isSuit(suit) { + if (suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣") { + return true; + } + return false; +} + function getCardValue(card) { - // TODO: Implement this function + // the length has to be 2 or 3 then we pass this check + + if (typeof card !== "string") { + throw new Error( + `Expected type for ${card} is string, but got ${typeof card}` + ); + } + + if (card.length !== 2 && card.length !== 3) { + throw new Error(`Invalid input length for ${card}`); + } + + const lastCharIndex = card.length - 1; + const rank = card.slice(0, lastCharIndex); + const suit = card.slice(lastCharIndex); + + if (!isSuit(suit)) { + throw new Error( + `Invalid last character detected, only suits(♠♥♦♣) are allowed, but got this character: ${suit}` + ); + } + + if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) { + // check if number and within bounds + return Number(rank); + } else { + throw new Error( + `Invalid rank "${rank}" in card "${card}": expected A, J, Q, K, or a number between 2 and 10` + ); + } } // The line below allows us to load the getCardValue function into tests in other files. 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..2da7b6d62b 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,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle == 90)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse" when (90 < angle < 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(140)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle == 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(220)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle"`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType("Test data")).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..9e922c4456 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,9 @@ 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(2, 2)).toEqual(false); + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(3, 4)).toEqual(true); + expect(isProperFraction(4312, 5000)).toEqual(true); + expect(isProperFraction("invalid", "data")).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..0b2ffeb397 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,36 @@ 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("AA♠")).toThrow(Error); +}); + +// Case 2 +test(`Should return 10 when given a jack, queen, king`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); + expect(() => getCardValue("K🤣")).toThrow( + "Invalid last character detected, only suits(♠♥♦♣) are allowed, but got this character:" + ); + expect(() => getCardValue("KKW🤣")).toThrow(Error); +}); + +// Case 3 +test(`Should return number when given a number`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("10♥")).toEqual(10); + expect(() => getCardValue("0♠")).toThrow(Error); + expect(() => getCardValue("1♠")).toThrow(Error); + expect(() => getCardValue("10Q")).toThrow(Error); + expect(() => getCardValue("423🤣")).toThrow(Error); +}); + +test("Should return error on invalid input", () => { + expect(() => getCardValue(null)).toThrow(Error); + expect(() => getCardValue(undefined)).toThrow(Error); + expect(() => getCardValue(12398)).toThrow(Error); + expect(() => getCardValue(NaN)).toThrow(Error); + expect(() => getCardValue("♠")).toThrow(Error); }); // Suggestion: Group the remaining test data into these categories: @@ -17,4 +47,3 @@ test(`Should return 11 when given an ace 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 -