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..68dd202ac7 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 970cb9b641..5d5b289d99 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,11 @@ 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 +36,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); 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 c7559e787e..5f3e11e770 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,15 @@ function getCardValue(card) { // TODO: Implement this function + const cardRank = card.slice(0, -1); + const cardSuit = card.slice(-1); + if (card.length < 2 || !["♠", "♥", "♦", "♣"].includes(cardSuit)) + throw new Error("invalid card suit"); + if (cardRank === "A") return 11; + if (["J", "Q", "K"].includes(cardRank)) return 10; + if (["2", "3", "4", "5", "6", "7", "8", "9", "10"].includes(cardRank)) + return Number(cardRank); // the parseint() or Number() can be used to convert the string to a number + throw new Error("invalid card rank"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -41,6 +50,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 +64,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 d777f348d3..8b788a7d0b 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 7f087b2ba1..f016943068 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,33 @@ 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: both numerator and denominator are zeros (undefined) + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// Special case: numerator is zero (proper fraction) +test(`should return true for proper fraction`, () => { + expect(isProperFraction(0, 1)).toEqual(true); + // case: both values are positive and the value of the numerater is less than the value of the denominater (proper fraction) + expect(isProperFraction(1, 2)).toEqual(true); + // case: the value of the numerater is negative and its absolute value is less than the value of the denominater (roper fraction) + expect(isProperFraction(-1, 2)).toEqual(true); + // case: the value of the denominator is negative and its absolute value is greater than the value of the numerator (proper fraction) + expect(isProperFraction(1, -2)).toEqual(true); + // case: both values of the numerator and denominator are negative and the absolute value of the numerator is less than the absolute value of the denominator (proper fraction) + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +// case: both values are positive and the value of the denominater is less than the value of the numerater (improper fraction) +test(`should return false for improper fraction`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + // case: the value of the numerater is negative and its absolute value is greater than the value of the denominater (improper fraction) + expect(isProperFraction(-2, 1)).toEqual(false); + // case: both values of the numerator and denominator are negative and the absolute value of the numerator is greater than the absolute value of the denominator (improper fraction) + expect(isProperFraction(-2, -1)).toEqual(false); + // case: the value of the denominator is negative and its absolute value is less than the value of the numerator (improper fraction) + expect(isProperFraction(2, -1)).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..f73e70e55d 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,14 +7,44 @@ 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: // Number Cards (2-10) +test("should return the value of number cards (2-10)", () => { + 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 the value of face cards (J, Q, K)`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); +}); // Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("1♠")).toThrow(); + expect(() => getCardValue("2❦")).toThrow(); + expect(() => getCardValue("11♥")).toThrow(); + expect(() => getCardValue("B♦")).toThrow(); + expect(() => getCardValue("Z♣")).toThrow(); + expect(() => getCardValue("♣2")).toThrow(); + expect(() => getCardValue("s")).toThrow(); + expect(() => getCardValue("♣♥♠♦")).toThrow(); + 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 -