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..4b20ede481 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 @@ -1,29 +1,41 @@ // Implement a function getAngleType // -// When given an angle in degrees, it should return a string indicating the type of angle: +// When given an angle in degrees, it should return a string: // - "Acute angle" for angles greater than 0° and less than 90° // - "Right angle" for exactly 90° // - "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. +// - "Invalid angle" for angles outside the valid range -// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) +// Assumption: The parameter is a valid number. (You do not need to check this.) // Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and +// After you have implemented the function, write tests to cover all cases, including boundary values, and // 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. -// This will be useful in the "rewrite tests with jest" step. +// The line below allows us to load the getAngleType function into other files +// This will be useful in the "rewrite tests with jest" step later 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 +// This helper function is written to make our assertions easier to read +// If the actual output matches the target output, the test passes function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -31,7 +43,13 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases, including boundary and invalid cases. +// TODO: Write tests to cover all cases, including boundary values // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +const invalid = getAngleType(-10); +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..a47f2a6468 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 @@ -1,21 +1,21 @@ // Implement a function isProperFraction, -// when given two numbers, a numerator and a denominator, it should return true if +// when given two numbers, a numerator and a denominator, returns true if // the given numbers form a proper fraction, and false otherwise. -// Assumption: The parameters are valid numbers (not NaN or Infinity). +// Assumption: The parameters are valid numbers (not NaN or infinity). -// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition. +// Note: If you are unfamiliar with proper fractions, please look it up. // Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and +// After you have implemented the function, write tests to // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + 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. +// The line below allows us to load the isProperFraction function into other files +// This will be useful in the "rewrite tests with jest" step later module.exports = isProperFraction; // Here's our helper again @@ -27,7 +27,11 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all cases. -// What combinations of numerators and denominators should you test? +// What combinations of numerators and denominators should be tested? // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(3, 3), 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..c62b8b6120 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,31 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + const validSuits = ["♠", "♥", "♦", "♣"]; + + let rank; + let suit; + + if (card.startsWith("10")) { + rank = "10"; + suit = card.slice(2); + } else { + rank = card.slice(0, 1); + suit = card.slice(1); + } + + if (!validRanks.includes(rank) || !validSuits.includes(suit)) { + throw new Error(`Invalid card: ${card}`); + } + + 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. @@ -40,15 +64,18 @@ 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("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 😢"); + console.error("Error was not thrown for invalid card 😞"); } catch (e) { console.log("Error thrown for invalid card 🎉"); } -// What other invalid card cases can you think of? +// What other invalid card cases can you think of? \ No newline at end of file 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..fc3c502f7e 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 @@ -6,7 +6,7 @@ const getAngleType = require("../implement/1-get-angle-type"); // including boundary and invalid cases. // Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { +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"); @@ -14,7 +14,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // 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(135)).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(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test("should return \"Invalid angle\" for angles outside the valid range", () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-10)).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..6f3523a53a 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,24 @@ 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`, () => { +// Special case: denominator is zero +test('should return false when denominator is zero', () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Case: proper fractions +test('should return true when the fraction is proper', () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(-1, 2)).toEqual(true); +}); + +// Case: improper fractions +test('should return false when the fraction is improper', () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(3, 3)).toEqual(false); +}); + +// Case: numerator is zero +test('should return true when numerator is zero', () => { + expect(isProperFraction(0, 5)).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..bb896a27b0 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 @@ -5,10 +5,32 @@ 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`, () => { +test("Should return 11 when given an ace card", () => { expect(getCardValue("A♠")).toEqual(11); }); +// 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); +}); + +// Case 3: Face cards (J, Q, K) +test("should return 10 for face cards", () => { + 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("1♠")).toThrow(); + expect(() => getCardValue("A")).toThrow(); + expect(() => getCardValue("K")).toThrow(); +}); + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K) @@ -16,5 +38,4 @@ 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 - +// https://jestjs.io/docs/expect#tothrowerror \ No newline at end of file