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..68dd202ac 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. @@ -35,3 +47,32 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +// Identify acute Angles +const acute = getAngleType(60); +assertEquals(acute, "acute angle"); + +// Identify obtuse Angles +const obtuse = getAngleType(120); +assertEquals(obtuse, "obtuse angle"); + +// Identify straight Angles +const straight = getAngleType(180); +assertEquals(straight, "straight angle"); + +// Identify reflex Angles +const reflex = getAngleType(250); +assertEquals(reflex, "reflex angle"); + +// Identify invalid Angles +const invalid = getAngleType(0); +assertEquals(invalid, "invalid angle"); + +const invalid = getAngleType(360); +assertEquals(invalid, "invalid angle"); + +const invalid = getAngleType(-100); +assertEquals(invalid, "invalid angle"); + +const invalid = getAngleType(1000); +assertEquals(invalid, "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..0cdc3126d 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 (Math.abs(numerator) < Math.abs(denominator)) { + return true; + } else { + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +35,15 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +// Example: 2/1 is not a proper fraction +assertEquals(isProperFraction(2, 1), false); +// Example: 1/2 is a proper fraction +assertEquals(isProperFraction(-1, 2), true); +// Example: -1/2 is a proper fraction +assertEquals(isProperFraction(-1, -2), true); +// Example: 1/2 is a proper fraction +assertEquals(isProperFraction(1, -2), true); +// Example: -2/1 is not a proper fraction +assertEquals(isProperFraction(-2, 1), false); +// Example: 1/2 is not a proper fraction +assertEquals(isProperFraction(-2, -1), 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..049046ef1 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,17 @@ function getCardValue(card) { // TODO: Implement this function + if ( + card.length < 2 || + card.length > 3 || + !["♠", "♥", "♦", "♣"].includes(card.slice(-1)) + ) + throw new Error("invalid suit"); + if (card[0] === "A") return 11; + if (["J", "Q", "K"].includes(card[0])) return 10; + if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(card[0])) + return card[0]; // the parseint() or Number() can be used to convert the string to a number + throw new Error("invalid rank"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -41,6 +52,11 @@ function assertEquals(actualOutput, targetOutput) { // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("A♥"), 11); +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("2♦"), 2); + // Handling invalid cards try { getCardValue("invalid"); @@ -50,3 +66,37 @@ try { } catch (e) {} // What other invalid card cases can you think of? +try { + getCardValue("S"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("AJKP"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("A❦"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("29"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("♦,♣"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("2345"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue(""); + console.error("Error was not thrown for invalid card"); +} 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..8b788a7d0 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 is 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(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is 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" when (angle <= 0 or angle >= 360)`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).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 7f087b2ba..a8e22c14a 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,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 +// Special case: denominator is zero (undefined) test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Special case: numerator is zero (proper fraction) +test(`should return true when numerator is zero`, () => { + expect(isProperFraction(0, 1)).toEqual(true); +}); + +// Special case: both numerator and denominator are zeros (undefined) +test(`should return false when both numerator and denominator are zero`, () => { + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// case: the absolute value of the numerater is less than the absolute value of the denominater (proper fraction) +test(`should return true for positive proper fraction`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +// case: the absolute value of the denominater is less than the absolute value of the numerater (improper fraction) +test(`should return false for positive improper fraction`, () => { + expect(isProperFraction(2, 1)).toEqual(false); +}); + +// no need to test the cases where the denominator or numerator or both are negative as I used their absolute value in the function. 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..6b0551151 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 @@ -8,13 +8,87 @@ const getCardValue = require("../implement/3-get-card-value"); test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +test(`Should return 11 when given an ace card`, () => { + expect(getCardValue("A♥")).toEqual(11); +}); +test(`Should return 11 when given an ace card`, () => { + expect(getCardValue("A♦")).toEqual(11); +}); +test(`Should return 11 when given an ace card`, () => { + expect(getCardValue("A♣")).toEqual(11); +}); // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return 2 when given a 2 card`, () => { + expect(getCardValue("2♠")).toEqual(2); +}); +test(`Should return 3 when given a 3 card`, () => { + expect(getCardValue("3♥")).toEqual(3); +}); +test(`Should return 4 when given a 4 card`, () => { + expect(getCardValue("4♦")).toEqual(4); +}); +test(`Should return 5 when given a 5 card`, () => { + expect(getCardValue("5♣")).toEqual(5); +}); +test(`Should return 6 when given a 6 card`, () => { + expect(getCardValue("6♥")).toEqual(6); +}); +test(`Should return 7 when given a 7 card`, () => { + expect(getCardValue("7♠")).toEqual(7); +}); +test(`Should return 8 when given a 8 card`, () => { + expect(getCardValue("8♦")).toEqual(8); +}); +test(`Should return 9 when given a 9 card`, () => { + expect(getCardValue("9♠")).toEqual(9); +}); +test(`Should return 10 when given a 10 card`, () => { + expect(getCardValue("10♣")).toEqual(10); +}); + // Face Cards (J, Q, K) +test(`Should return 10 when given a J card`, () => { + expect(getCardValue("J♠")).toEqual(10); +}); +test(`Should return 10 when given a Q card`, () => { + expect(getCardValue("Q♦")).toEqual(10); +}); +test(`Should return 10 when given a K card`, () => { + expect(getCardValue("K♥")).toEqual(10); +}); // Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("1♠")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("2❦")).toThrow(); +}); + +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("11♥")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("B♦")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("Z♣")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("♣2")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("s")).toThrow(); +}); +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("♣♥♠♦")).toThrow(); +}); + +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("")).toThrow(); +}); // 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 -