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..27616acae 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,7 +16,20 @@ 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. // This will be useful in the "rewrite tests with jest" step. @@ -35,3 +48,13 @@ 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(400); +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..0769a29b0 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,17 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { + + // TODO: Implement this function + if (denominator === 0) { + return false; + } + 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. @@ -24,6 +34,7 @@ function assertEquals(actualOutput, targetOutput) { actualOutput === targetOutput, `Expected ${actualOutput} to equal ${targetOutput}` ); + } // TODO: Write tests to cover all cases. @@ -31,3 +42,11 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(0, 2), true); +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(-2, -1), false); +assertEquals(isProperFraction(-2, -2), false); +assertEquals(isProperFraction(1, 0), 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 c7559e787..74878e76d 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,24 @@ function getCardValue(card) { // TODO: Implement this function + const rank = card.slice(0, -1); // Get the rank by slicing off the last character (the suit) + const suit = card.slice(-1); // Get the suit by taking the last character of the string + + // Validate the suit + const validSuits = ["♠", "♥", "♦", "♣"]; + if (!validSuits.includes(suit)) { + throw new Error("Invalid card: Invalid suit"); + } + // Determine the value based on the rank + if (rank === "A") { + return 11; + } else if (["J", "Q", "K"].includes(rank)) { + return 10; + } else if (!isNaN(rank) && parseInt(rank) >= 2 && parseInt(rank) <= 10) { + return parseInt(rank); + } else { + throw new Error("Invalid card: Invalid rank"); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,13 +58,28 @@ 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("J♦"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("10♥"), 10); -// Handling invalid cards -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) {} +// Test invalid cards - these should throw errors +function testInvalidCard(card) { + try { + getCardValue(card); + console.error(`Error was not thrown for invalid card: ${card}`); + } catch (e) { + console.log(`✓ Correctly threw error for: ${card}`); + } +} -// What other invalid card cases can you think of? +testInvalidCard("11♠"); // Invalid rank +testInvalidCard("A"); // Missing suit +testInvalidCard("2X"); // Invalid suit +testInvalidCard("invalid"); // Completely invalid +testInvalidCard(""); // Empty string +testInvalidCard("A♠♠"); // Extra character +testInvalidCard("1♠"); // Invalid rank (1 instead of A) +testInvalidCard("B♠"); // Invalid rank +testInvalidCard("A♠A"); // Invalid format 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..2a2be38ee 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 @@ -5,16 +5,41 @@ 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 identify acute angles", () => { expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); // Case 2: Right angle +test("should identify right angles", () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test("should identify obtuse angles", () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test("should identify straight angles", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test("should identify reflex angles", () => { + expect(getAngleType(190)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test("should identify invalid angles", () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType(400)).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..11f2c3ed9 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,27 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); +// Case 1: Proper fractions +test(`should return true for proper fractions`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(true); + expect(isProperFraction(0, 2)).toEqual(true); +}); + +// Case 2: Improper fractions +test(`should return false for improper fractions`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(-2, 1)).toEqual(false); + expect(isProperFraction(2, -1)).toEqual(false); + expect(isProperFraction(-2, -1)).toEqual(false); + expect(isProperFraction(2, 2)).toEqual(false); + expect(isProperFraction(-2, -2)).toEqual(false); +}); +// Case 3: Invalid fractions (denominator is zero) +test(`should return false when denominator is zero`, () => { + expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(-1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).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..87419c87b 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 @@ -11,10 +11,19 @@ test(`Should return 11 when given an ace card`, () => { // 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 10 when given a 10 card`, () => { + expect(getCardValue("10♠")).toEqual(10); +}); // Face Cards (J, Q, K) // Invalid Cards - +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("11♠")).toThrow("Invalid card: Invalid rank"); + expect(() => getCardValue("A")).toThrow("Invalid card: Invalid suit"); + expect(() => getCardValue("2X")).toThrow("Invalid card: Invalid suit"); +}); // 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 -