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..1f3383ff62 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" + } if (angle === 90) { + return "Right angle" + } if (angle > 90 && angle < 180) { + return "Obtuse angle" + } if (angle === 180) { + return "Straight angle" + } 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. @@ -25,13 +37,44 @@ module.exports = getAngleType; // This helper function is written to make our assertions easier to read. // If the actual output matches the target output, the test will pass function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${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"); + +const invalid = getAngleType(0); +assertEquals(invalid, "Invalid angle"); + +const acute = getAngleType(89); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(91); +assertEquals(obtuse, "Obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle") + +const invalid1 = getAngleType(360); +assertEquals(invalid1, "Invalid angle") + +const acute1 = getAngleType(1); +assertEquals(acute1, "Acute angle") + +const obtuse1 = getAngleType(179); +assertEquals(obtuse1, "Obtuse angle") + +const reflex = getAngleType(181); +assertEquals(reflex, "Reflex angle") + +const reflex1 = getAngleType(359); +assertEquals(reflex1, "Reflex angle") + +const invalid2 = getAngleType(-1); +assertEquals(invalid2, "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..6f39a23f8c 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,7 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return denominator !== 0 && Math.abs(numerator / denominator) < 1; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -26,8 +26,19 @@ 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, 1), false); +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(0, 5), true); +assertEquals(isProperFraction(5, 0), false); +assertEquals(isProperFraction(0, 0), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(-2, -1), false); +assertEquals(isProperFraction(-1, -2), true); +assertEquals(isProperFraction(0.5, 1), true); +assertEquals(isProperFraction(1.5, 1), false); +assertEquals(isProperFraction(999999999, 1000000000), true); 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..a81698bc03 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,43 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + if (card === "") { + throw new Error("No card was played"); + } + if (card.length < 2 || card.length > 3) { + throw new Error("Invalid card"); + } + + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; + + const suit = card.slice(-1); + if (!validSuits.includes(suit)) { + throw new Error("Invalid card: suit is not recognised"); + } + + const rank = card.slice(0, -1).toUpperCase(); + if (!validRanks.includes(rank)) { + throw new Error("Invalid card: rank is not recognised"); + } + if (rank === "A") return 11; + if (["J", "Q", "K"].includes(rank)) return 10; + + return Number(rank); } // The line below allows us to load the getCardValue function into tests in other files. @@ -37,18 +73,58 @@ 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("A♥"), 11); +assertEquals(getCardValue("A♦"), 11); +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("3♥"), 3); +assertEquals(getCardValue("4♦"), 4); +assertEquals(getCardValue("5♣"), 5); +assertEquals(getCardValue("6♠"), 6); +assertEquals(getCardValue("7♥"), 7); +assertEquals(getCardValue("8♦"), 8); +assertEquals(getCardValue("9♣"), 9); +assertEquals(getCardValue("10♠"), 10); +assertEquals(getCardValue("J♠"), 10); +assertEquals(getCardValue("Q♥"), 10); +assertEquals(getCardValue("K♦"), 10); // Handling invalid cards try { - getCardValue("invalid"); + getCardValue(""); // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card 😢"); } catch (e) { - console.log("Error thrown for invalid card 🎉"); + console.log(e); +} + +// What other invalid card cases can you think of? +try { + getCardValue("100"); + console.error("Error was not thrown for card with more than 3 in length"); +} catch (e) { + console.log(e); +} +try { + getCardValue("1"); + console.error("Error was not thrown for a card.lenght = 1"); +} catch (e) { + console.log(e); } -// What other invalid card cases can you think of? +try { + getCardValue("♦"); + console.error("Error was not thrown for a card play of just suits"); +} catch (e) { + console.log(e); +} + +try { + getCardValue("A😊"); + console.error("Error was not thrown for a card play of a wrong suit"); +} catch (e) { + console.log(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..d387b6f103 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 @@ -2,19 +2,35 @@ // We will use the same function, but write tests for it using Jest in this file. const getAngleType = require("../implement/1-get-angle-type"); -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. - // Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { - // Test various acute angles, including boundary cases +test(`should return "Acute angle" when angle is greater than 0 or angle is less than 90')`, () => { expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); -// Case 2: Right angle -// Case 3: Obtuse angles +// Case 2: Obtuse angle +test(`should return "obtuse angle" when angle is greater than 90 but less than 180 `, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); +// Case 3: Right angles +test(`should return "Right angle" when (angle === 90)`, () => { + expect(getAngleType(90)).toEqual("Right 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 "Reflect angle" when angle is greater than 180 but less than 360))`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`"should return 'Invalid angle' when angle is 0, 360, less than 0, or greater than 360")`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-1)).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..edadbd0cda 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 @@ -2,9 +2,51 @@ // We will use the same function, but write tests for it using Jest in this file. 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. +// Proper fraction +test("should return true when the denominator is greater than the numerator", () => { + expect(isProperFraction(1, 2)).toBe(true); +}); + +// Improper fraction +test("should return false when the numerator is greater than the denominator", () => { + expect(isProperFraction(2, 1)).toBe(false); +}); + +// Zero numerator +test("should return true when the numerator is zero", () => { + expect(isProperFraction(0, 5)).toBe(true); +}); + +// Zero denominator +test("should return false when the denominator is zero", () => { + expect(isProperFraction(5, 0)).toBe(false); +}); + +// numerator equals denominator +test("should return false when numerator equals denominator ", () => { + expect(isProperFraction(5, 5)).toBe(false); +}); + +// Both numerator and denominator are zero +test("should return false when both the numerator and denominator are zero", () => { + expect(isProperFraction(0, 0)).toBe(false); +}); + +// Negative numbers +test("should correctly identify proper fractions when given negative numbers", () => { + expect(isProperFraction(-1, 2)).toBe(true); + expect(isProperFraction(1, -2)).toBe(true); + expect(isProperFraction(-1, -2)).toBe(true); + expect(isProperFraction(-2, -1)).toBe(false); +}); + +// Decimal numbers +test("should correctly identify proper fractions when given decimal numbers", () => { + expect(isProperFraction(0.5, 1)).toBe(true); + expect(isProperFraction(1.5, 1)).toBe(false); +}); -// Special case: numerator is zero -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); +// Large numbers +test("should correctly identify proper fractions when given very large numbers", () => { + expect(isProperFraction(999999999, 1000000000)).toBe(true); }); 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..de63ffb36a 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 @@ -2,19 +2,38 @@ // We will use the same function, but write tests for it using Jest in this file. const getCardValue = require("../implement/3-get-card-value"); -// TODO: Write tests in Jest syntax to cover all possible outcomes. - -// Case 1: Ace (A) +// Ace (A) 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) -// Face Cards (J, Q, K) -// Invalid Cards +test(`Should return the card's numeric rank for cards 2 through 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); +}); -// 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 +// Face Cards (J, Q, K) +test(`should return 10 When the card is a face card ("J", "Q", "K")`, () => { + expect(getCardValue("j♠")).toEqual(10); + expect(getCardValue("k♦")).toEqual(10); + expect(getCardValue("q♣")).toEqual(10); +}); +// Invalid Cards +test("should throw an error when an invalid card is played", () => { + expect(() => getCardValue("")).toThrow("No card was played"); + expect(() => getCardValue("X♠")).toThrow( + "Invalid card: rank is not recognised" + ); + expect(() => getCardValue("9X")).toThrow( + "Invalid card: suit is not recognised" + ); +});