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..1439c70d10 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,21 @@ function getAngleType(angle) { // TODO: Implement this function + let returnAngle; + if (angle > 0 && angle < 90) { + returnAngle = "Acute"; + } else if (angle === 90) { + returnAngle = "Right"; + } else if (angle > 90 && angle < 180) { + returnAngle = "Obtuse"; + } else if (angle === 180) { + returnAngle = "Straight"; + } else if (angle > 180 && angle < 360) { + returnAngle = "Reflex"; + } else { + returnAngle = "Invalid"; + } + return `${returnAngle} angle`; } // The line below allows us to load the getAngleType function into tests in other files. @@ -34,4 +49,17 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles const right = getAngleType(90); +const acute = getAngleType(75); +const obtuse = getAngleType(150); +const straight = getAngleType(180); +const reflex = getAngleType(340); +let invalid = getAngleType(0); +invalid = getAngleType(360); +invalid = getAngleType(-2); + assertEquals(right, "Right angle"); +assertEquals(acute, "Acute angle"); +assertEquals(obtuse, "Obtuse angle"); +assertEquals(straight, "Straight angle"); +assertEquals(reflex, "Reflex angle"); +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..d2b69b0970 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. @@ -31,3 +33,9 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(3, 1), false); +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(5, 0), false); +assertEquals(isProperFraction(-1, 0), false); +assertEquals(isProperFraction(-2, 4), true); +assertEquals(isProperFraction(4, -2), 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..92a6416e0a 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,28 @@ function getCardValue(card) { // TODO: Implement this function + const cardRanks = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(","); + const cardSuits = "♠,♥,♦,♣".split(","); + + // Throw error if card is empty + if (!card) throw new Error(`Invalid card`); + + const cardSuit = card[card.length - 1]; + // Throw error is card suit is not valid + if (!cardSuits.includes(cardSuit)) { + throw new Error(`Invalid card suit`); + } + + const cardRank = card.slice(0, -1); + // Throw error is cardRank is not valid + if (!cardRanks.includes(cardRank)) { + throw new Error(`Invalid card rank`); + } + + // Return the card value based on given rank + if (cardRank === "A") return 11; + if (["J", "Q", "K"].includes(cardRank)) return 10; + return Number(cardRank); } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,15 +62,40 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("J♥"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♠"), 10); +assertEquals(getCardValue("A♥"), 11); +assertEquals(getCardValue("10♦"), 10); // Handling invalid cards -try { - getCardValue("invalid"); +function assertErrors(func, errorMessage) { + try { + func(); + console.error("Error was not thrown for invalid card 😢"); + } catch (error) { + console.assert( + error.message.includes(errorMessage), + `Expected error: ${errorMessage}, got: ${error.message}` + ); + console.log("Error thrown for invalid card 🎉"); + } +} + +function testInvalidCards() { + const invalidCardCases = [ + ["A", "Invalid card suit"], + ["11♠", "Invalid card rank"], + ["N♠", "Invalid card rank"], + ["7K", "Invalid card suit"], + ["", "Invalid card"], + ]; - // 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 🎉"); + invalidCardCases.forEach(([card, message]) => { + console.log("-".repeat(50)); + console.log(`Card: ${card}, Message: ${message}`); + assertErrors(() => getCardValue(card), message); + }); } -// What other invalid card cases can you think of? +testInvalidCards(); 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..95c5cab8fd 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,28 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when angle === 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(120)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse 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 "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(340)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid" when (angle < 1 || angel > 360)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-15)).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..85201d254b 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 @@ -8,3 +8,13 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +test(`should return false when (numerator >= denominator)`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(2, 2)).toEqual(false); +}); + +test(`should treat numerator and denominator as absolute values when determining proper fractions`, () => { + expect(isProperFraction(-2, 3)).toEqual(true); + expect(isProperFraction(-7, 2)).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..8f9adab101 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 @@ -9,6 +9,31 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +test(`Should return 10 when given an a face card of J, Q or K`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); +}); + +test(`Should return number when given an a face card (>= 2 && < 10)`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("6♠")).toEqual(6); + expect(getCardValue("9♠")).toEqual(9); +}); + +// Invalid cards +test(`Should return "Invalid card suit" when suit is invalid`, () => { + expect(() => getCardValue("A")).toThrowError(); +}); + +test(`Should return "Invalid card rank" when rank is invalid`, () => { + expect(() => getCardValue("1♠")).toThrowError(); +}); + +test(`Should return "Invalid card" when rank is an empty card`, () => { + expect(() => getCardValue("")).toThrowError(); +}); + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K) @@ -17,4 +42,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 - diff --git a/package.json b/package.json deleted file mode 100644 index 0657e22dd8..0000000000 --- a/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "module-structuring-and-testing-data", - "version": "1.0.0", - "description": "Like learning a musical instrument, programming requires daily practice.", - "main": "index.js", - "scripts": { - "test": "jest" - }, - "keywords": [], - "author": "Code Your Future", - "license": "ISC", - "dependencies": { - "jest": "^29.7.0" - } -} \ No newline at end of file