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..5492c5a437 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 @@ -6,7 +6,8 @@ // - "Obtuse angle" for angles greater than 90° and less than 180° // - "Straight angle" for exactly 180° // - "Reflex angle" for angles greater than 180° and less than 360° -// - "Invalid angle" for angles outside the valid range. +// - "(angle >= 0 || angle <= 90){ +// Invalid angle" for angles outside the valid range. // Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) @@ -15,7 +16,17 @@ // 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. @@ -33,5 +44,19 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); const right = getAngleType(90); assertEquals(right, "Right angle"); +const obtuse = getAngleType(110); +assertEquals(obtuse, "Obtuse angle"); +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +const reflex = getAngleType(250); +assertEquals(reflex, "Reflex angle"); +const invalid = getAngleType(380); +assertEquals(invalid, "Invalid angle"); +const invalid2 = getAngleType(0); +assertEquals(invalid2, "Invalid angle"); +const invalid3 = getAngleType(-10); +assertEquals(invalid3, "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..6dc8e753c9 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,11 +11,12 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0) { + return false; + } else 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. -// This will be useful in the "rewrite tests with jest" step. module.exports = isProperFraction; // Here's our helper again @@ -27,7 +28,24 @@ 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 +// numerator < denominator (in absolute value) → proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(8, 9), true); +assertEquals(isProperFraction(0, 5), true); // numerator = 0 + +// numerator > denominator (in absolute value) → improper fraction +assertEquals(isProperFraction(3, 2), false); +assertEquals(isProperFraction(-4, 1), false); + +// numerator = denominator (in absolute value) → not a proper fraction +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(-7, -7), false); + +// denominator = 0 → invalid fraction +assertEquals(isProperFraction(1, 0), false); +assertEquals(isProperFraction(0, 0), false); + +// negative numbers still follow the rule: numerator < denominator (in absolute value) +assertEquals(isProperFraction(2, -4), true); +assertEquals(isProperFraction(-2, -5), 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 c7559e787e..324981465d 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,9 +22,25 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function -} + if (typeof card !== "string") { + throw new Error("Invalid card"); + } + let rank = card.slice(0, -1); // Get everything except the last character + let suit = card.slice(-1); // Get the last character + + const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + if (rank === "A") { + return 11; + } else if (/^[JQK]$/.test(rank)) { + return 10; + } else if (rank.match(/^(10|[2-9])$/)) { + return Number(rank); + } else throw new Error("Invalid card"); +} // The line below allows us to load the getCardValue function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getCardValue; @@ -36,17 +52,48 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${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("3♦"), 3); -// Handling invalid cards try { - getCardValue("invalid"); + getCardValue("J"); - // This line will not be reached if an error is thrown as expected + // The below line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); -} catch (e) {} - +} catch (e) { + console.log('Test passed for "J": caught error ->', e.message); +} // What other invalid card cases can you think of? + +try { + getCardValue("9X"); // invalid suit + console.error('Test failed for "9X": error was not thrown'); +} catch (e) { + console.log('Test passed for "9X": caught error ->', e.message); +} + +try { + getCardValue("1♠"); // invalid rank + console.error('Test failed for "1♠": error was not thrown'); +} catch (e) { + console.log('Test passed for "1♠": caught error ->', e.message); +} + +try { + getCardValue("0♥"); // invalid rank + console.error('Test failed for "0♥": error was not thrown'); +} catch (e) { + console.log('Test passed for "0♥": caught error ->', e.message); +} + +try { + getCardValue("ABC"); // completely wrong format + console.error('Test failed for "ABC": error was not thrown'); +} catch (e) { + console.log('Test passed for "ABC": caught error ->', e.message); +} 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..1fa313ca17 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,31 @@ 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(150)).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(200)).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(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-40)).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 7f087b2ba1..6d6ae6b651 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,30 @@ 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 -test(`should return false when denominator is zero`, () => { +test("returns true when |numerator| < |denominator|", () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +test("returns true when numerator is zero", () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); + +test("returns false when |numerator| > |denominator|", () => { + expect(isProperFraction(3, 2)).toEqual(false); +}); + +test("returns false when |numerator| = |denominator|", () => { + expect(isProperFraction(5, 5)).toEqual(false); +}); + +test("returns false when denominator is zero", () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +test("returns true when denominator is negative but |numerator| < |denominator|", () => { + expect(isProperFraction(2, -4)).toEqual(true); +}); + +test("returns true when both numbers are negative but |numerator| < |denominator|", () => { + expect(isProperFraction(-3, -4)).toEqual(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..a10b60bbc9 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,9 +11,29 @@ 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 the value of rank variable as a number when we are given 2-10", ()=>{ + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("9♠")).toEqual(9); + expect(getCardValue("10♠")).toEqual(10); +}); // Face Cards (J, Q, K) +test(`Should return 10 when given J or Q or K`, () => { + expect(getCardValue("K♠")).toEqual(10); + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); +}) // Invalid Cards - +test("Should return Invalid card", ()=>{ + expect(() => getCardValue(0)).toThrow("Invalid card"); + expect(() => getCardValue("1X")).toThrow("Invalid card"); + expect(() => getCardValue("0♥")).toThrow("Invalid card"); +}); +// Invalid card format +test("Should throw error for tricky invalid card formats", () => { + expect(() => getCardValue("0x02♠")).toThrow("Invalid card"); + expect(() => getCardValue("QQ♠")).toThrow("Invalid card"); + expect(() => getCardValue("2.1♠")).toThrow("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