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..972a4a495 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 @@ -16,6 +16,32 @@ function getAngleType(angle) { // TODO: Implement this function + // if angle is between 0 and 90 then return acute angle + if (angle > 0 && angle < 90) { + return "Acute angle"; + } + + // if it is exactly 90 return right angle + else if (angle === 90) { + return "Right angle"; + } + // greater than 90 less than 180 return obtuse angle + else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + // if exactly 180 return straight angle + else if (angle === 180) { + return "Straight angle"; + } + // greater than 180 less than 360 return reflex angle + else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + // everything greater than 360 rand less than 0 return invalid angle + else { + return "Invalid angle"; + } + // return a string tells user which angle } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +61,18 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +const invalid = getAngleType(200); +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..3884d6558 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,8 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator === 0) return false; + return Math.abs(numerator) < Math.abs(denominator); } // The line below allows us to load the isProperFraction function into tests in other files. 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..775f9b2aa 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 @@ -22,7 +22,31 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const suits = ["♠", "♥", "♦", "♣"]; // valid card suits + + // must have a valid suit + if (!suits.includes(card.slice(-1))) { + throw new Error("Error invalid card"); + } + + const rank = card.slice(0, -1); // gets beginning character up to (not including) the last character and saves it in the rank variable. + + // if card is an ace return 11 + if (rank === "A") { + return 11; + } + + // if card is face "J", "Q", "K" + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + + const num = Number(rank); + // if card is a number card and card is bigger than 2 and less than 10 ("2" to "10"), should return its numerical value + if (!Number.isInteger(num) || rank !== String(num) || num < 2 || num > 10) { + throw new Error("Error invalid card"); + } + return num; } // The line below allows us to load the getCardValue function into tests in other files. @@ -39,14 +63,14 @@ 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 { - getCardValue("invalid"); +// try { +// getCardValue("invalid"); - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card"); -} catch (e) {} +// // This line will not be reached if an error is thrown as expected +// console.error("Error was not thrown for invalid card"); +// } catch (e) {} // 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 d777f348d..d8ea4be72 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,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle === 90)`, () => { + // Test various right angles, including boundary cases + expect(getAngleType(90)).toEqual("Right angle"); +}); // Case 3: Obtuse angles +test(`should return "Obtuse angles" when (angle > 90 && < 180)`, () => { + // Test various obtuse angles, including boundary cases + expect(getAngleType(96)).toEqual("Obtuse angle"); + expect(getAngleType(142)).toEqual("Obtuse angle"); + expect(getAngleType(178)).toEqual("Obtuse angle"); +}); // Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + // Test various straight angles, including boundary cases + expect(getAngleType(180)).toEqual("Straight angle"); +}); // Case 5: Reflex angles +test(`should return "Reflex angles" when (angle > 180 && < 360)`, () => { + // Test various reflex angles, including boundary cases + expect(getAngleType(199)).toEqual("Reflex angle"); + expect(getAngleType(245)).toEqual("Reflex angle"); + expect(getAngleType(306)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid angles" when (angle is <= 0 && >= 360)`, () => { + // Test various invalid angles, including boundary cases + expect(getAngleType(400)).toEqual("Invalid angle"); + expect(getAngleType(-7)).toBe("Invalid angle"); + expect(getAngleType(360)).toBe("Invalid angle"); + expect(getAngleType(0)).toBe("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..a218edffc 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,45 @@ 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 test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Case 2: Proper fractions +// the numerator is smaller than the denominator, so the fraction is a proper fraction +test(`should return true when numerator < denominator`, () => { + expect(isProperFraction(6, 7)).toEqual(true); + expect(isProperFraction(2, 4)).toEqual(true); +}); + +// Case 3: Improper fractions +// numerator is bigger than denominator, so the fraction is not proper +test("should return false when numerator is bigger than denominator", () => { + expect(isProperFraction(8, 6)).toEqual(false); + expect(isProperFraction(4, 2)).toEqual(false); +}); + +// Case 4: Negative numbers +// numerator or denominator is negative, but function compares absolute values +test("should return false when |numerator| < |denominator| positive numerator and negative denominator)", () => { + expect(isProperFraction(7, -3)).toEqual(false); +}); +test("should return true when |numerator| < |denominator| negative numerator and positive denominator)", () => { + expect(isProperFraction(-5, 9)).toEqual(true); +}); + +// Case 5: Numerator equals denominator fraction equals 1, so it is not proper +test("should return false when numerator is equal to denominator", () => { + expect(isProperFraction(6, 6)).toEqual(false); +}); + +// Case 6: Numerator just less than denominator fraction is slightly below 1, so it is proper +test("should return true when numerator is just less than denominator", () => { + expect(isProperFraction(2, 3)).toEqual(true); +}); + +// Case 7: Numerator greater than denominator fraction is slightly above 1, so it is not proper +test("should return false when numerator is greater than denominator", () => { + expect(isProperFraction(6, 5)).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..7399d2f56 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,34 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +// Case 2: Face (J, Q, K) +test(`Should return 10 when given an face card`, () => { + expect(getCardValue("J♣")).toEqual(10); +}); + +test(`Should return 10 when given an face card`, () => { + expect(getCardValue("Q♦")).toEqual(10); +}); + +test(`Should return 10 when given an face card`, () => { + expect(getCardValue("K♥")).toEqual(10); +}); +// Case 3: Number Cards (2-10) +test(`Should return numerical value when given an number card`, () => { + expect(() => getCardValue("5")).toThrow("Error invalid card"); +}); + +test(`Should return error if the card string is invalid`, () => { + expect(() => getCardValue("17")).toThrow("Error invalid card"); +}); + +// Case 4: Hex, Decimal Numbers +test(`Should return error if the rank is a hex input`, () => { + expect(() => getCardValue("0x02♠")).toThrow("Error invalid card"); + expect(() => getCardValue("2.1♠")).toThrow("Error invalid card"); + expect(() => getCardValue("0002♠")).toThrow("Error 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 -