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..5935016948 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 >= 360) { + return "Invalid angle"; + } else if (angle < 90) { + return "Acute angle"; + } else if (angle === 90) { + return "Right angle"; + } else if (angle < 180) { + return "Obtuse angle"; + } else if (angle === 180) { + return "Straight angle"; + } else { + return "Reflex angle"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -31,7 +43,39 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles +// Tests to cover all cases, including boundary and invalid cases. +console.log("Starting tests..."); + +// 1. Right angle const right = getAngleType(90); assertEquals(right, "Right angle"); + +// 2. Acute angle +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +// 3. Obtuse angle +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); + +// 4. Straight angle +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +// 5. Reflex angle +const reflex = getAngleType(250); +assertEquals(reflex, "Reflex angle"); + +// 6. Invalid angle - zero +const invalidZero = getAngleType(0); +assertEquals(invalidZero, "Invalid angle"); + +// 7. Invalid angle - negative +const invalidNegative = getAngleType(-50); +assertEquals(invalidNegative, "Invalid angle"); + +// 8. Invalid angle - 360 or more +const invalidTooBig = getAngleType(360); +assertEquals(invalidTooBig, "Invalid angle"); + +console.log("All tests completed!"); 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..b4494cb4f0 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 @@ -4,21 +4,21 @@ // Assumption: The parameters are valid numbers (not NaN or Infinity). -// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. - // Acceptance criteria: // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. 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. -// This will be useful in the "rewrite tests with jest" step. module.exports = isProperFraction; -// Here's our helper again +// Helper function function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -26,8 +26,30 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases. -// What combinations of numerators and denominators should you test? +console.log("Starting tests for isProperFraction..."); -// Example: 1/2 is a proper fraction +// 1. Proper fraction: 1/2 assertEquals(isProperFraction(1, 2), true); + +// 2. Improper fraction: numerator greater than denominator +assertEquals(isProperFraction(3, 2), false); + +// 3. Improper fraction: numerator equals denominator +assertEquals(isProperFraction(5, 5), false); + +// 4. Proper fraction with negative numerator +assertEquals(isProperFraction(-1, 4), true); + +// 5. Proper fraction with negative denominator +assertEquals(isProperFraction(2, -5), true); + +// 6. Improper fraction with negative numerator +assertEquals(isProperFraction(-7, 3), false); + +// 7. Zero numerator — 0/5 is proper because 0 < 5 +assertEquals(isProperFraction(0, 5), true); + +// 8. Zero denominator — invalid, return false +assertEquals(isProperFraction(1, 0), false); + +console.log("All tests completed!"); 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..61398b19b3 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,14 +22,47 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card format"); + } + + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = [ + "A", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + ]; + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + if (!validSuits.includes(suit) || !validRanks.includes(rank)) { + throw new Error("Invalid card format"); + } + + if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + return Number(rank); + } } // 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; -// Helper functions to make our assertions easier to read. +// Helper function for value assertions function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -37,16 +70,43 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: +// Helper function to assert that an error is thrown +function assertThrowsError(invalidInput) { + let errorThrown = false; + try { + getCardValue(invalidInput); + } catch (e) { + errorThrown = true; + } + console.assert( + errorThrown, + `Expected an error to be thrown for input: "${invalidInput}"` + ); +} + +console.log("Starting tests for getCardValue..."); + +// --- Number cards --- +assertEquals(getCardValue("2♥"), 2); assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("10♥"), 10); + +// --- Face cards --- +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♦"), 10); -// Handling invalid cards -try { - getCardValue("invalid"); +// --- Ace card --- +assertEquals(getCardValue("A♠"), 11); - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card"); -} catch (e) {} +// --- Invalid cards --- +assertThrowsError("invalid"); // random string +assertThrowsError("10"); // missing suit +assertThrowsError("A"); // missing suit +assertThrowsError("♠"); // missing rank +assertThrowsError("1♠"); // invalid rank +assertThrowsError("11♠"); // invalid rank +assertThrowsError("AX"); // invalid suit +assertThrowsError(""); // empty string -// What other invalid card cases can you think of? +console.log("All tests completed!"); 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..43a97d88a1 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,41 @@ // 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 expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); // Case 2: Right angle +test(`should return "Right angle" when angle is 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 is 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(250)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when angle is <= 0 or >= 360`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-50)).toEqual("Invalid angle"); + expect(getAngleType(360)).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..e03c94c1fd 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,37 @@ // 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. +// 1. Proper fractions (absolute numerator < absolute denominator) +test(`should return true for proper fractions (positive and negative)`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(-1, 4)).toEqual(true); + expect(isProperFraction(3, -5)).toEqual(true); + expect(isProperFraction(-2, -7)).toEqual(true); +}); + +// 2. Improper fractions (absolute numerator > absolute denominator) +test(`should return false for improper fractions where numerator is greater than denominator`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(-5, 4)).toEqual(false); + expect(isProperFraction(7, -3)).toEqual(false); +}); + +// 3. Improper fractions (absolute numerator == absolute denominator) +test(`should return false when numerator equals denominator`, () => { + expect(isProperFraction(5, 5)).toEqual(false); + expect(isProperFraction(-5, 5)).toEqual(false); + expect(isProperFraction(5, -5)).toEqual(false); +}); + +// 4. Special case: numerator is zero +test(`should return true when numerator is zero and denominator is not zero`, () => { + expect(isProperFraction(0, 5)).toEqual(true); + expect(isProperFraction(0, -5)).toEqual(true); +}); -// Special case: numerator is zero +// 5. Special case: 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 cf7f9dae2e..f221fa6adc 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,33 @@ // 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) 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 +// Case 2: Number Cards (2-10) +test(`Should return the numeric value for number cards`, () => { + expect(getCardValue("2♥")).toEqual(2); + 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 +// Case 3: Face Cards (J, Q, K) +test(`Should return 10 for face cards (J, Q, K)`, () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♥")).toEqual(10); +}); +// Case 4: Invalid Cards +test(`Should throw an error for invalid cards`, () => { + expect(() => getCardValue("invalid")).toThrow(); + expect(() => getCardValue("10")).toThrow(); + expect(() => getCardValue("A")).toThrow(); + expect(() => getCardValue("♠")).toThrow(); + expect(() => getCardValue("1♠")).toThrow(); + expect(() => getCardValue("11♠")).toThrow(); + expect(() => getCardValue("AX")).toThrow(); + expect(() => getCardValue("")).toThrow(); +});