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 ca1dfe7f2..01ba45fbe 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 @@ -8,11 +8,39 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { + if (typeof angle !== "number" && typeof angle !== "string") { + return "Input should be a number or a number in string"; + } + if (angle === null || isNaN(Number(angle))) { + return "Input should be a number or a number in string"; + } + angle = Number(angle); + if (angle < 0 || angle > 360) { + return "Angle should be between 0 and 360"; + } + if (angle === 0) { + return "Zero angle"; + } if (angle === 90) { return "Right angle"; } - // Run the tests, work out what Case 2 is testing, and implement the required code here. - // Then keep going for the other cases, one at a time. + // Run the tests, work out what Case 2 is testing, and implement the required code here. + // Then keep going for the other cases, one at a time. + if (angle < 90) { + return "Acute angle"; + } + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + if (angle === 180) { + return "Straight angle"; + } + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + if (angle === 360) { + return "Full rotation"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -51,13 +79,18 @@ assertEquals(acute, "Acute angle"); // Then the function should return "Obtuse angle" const obtuse = getAngleType(120); // ====> write your test here, and then add a line to pass the test in the function above +assertEquals(obtuse, "Obtuse angle"); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" // ====> write your test here, and then add a line to pass the test in the function above +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> write your test here, and then add a line to pass the test in the function above +const reflex = getAngleType(250); +assertEquals(reflex, "Reflex 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 a4739af77..8715df1ad 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 @@ -8,9 +8,13 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) { - return true; - } + if (typeof numerator !== "number" && typeof numerator !== "string") + return false; + if (typeof denominator !== "number" && typeof denominator !== "string") + return false; + if (isNaN(numerator) || isNaN(denominator)) return false; + if (denominator == 0) return false; // handle zero denominator case + return Math.abs(numerator) < Math.abs(denominator); } // The line below allows us to load the isProperFraction function into tests in other files. @@ -47,6 +51,7 @@ assertEquals(improperFraction, false); // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. const negativeFraction = isProperFraction(-4, 7); // ====> complete with your assertion +assertEquals(negativeFraction, true); // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 @@ -54,6 +59,70 @@ const negativeFraction = isProperFraction(-4, 7); // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. const equalFraction = isProperFraction(3, 3); // ====> complete with your assertion +assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? + +// Stretch 1: negative denominator +// Input: numerator = 2, denominator = -3 +// target output: true +// Explanation: The fraction 2/-3 is a proper fraction because the absolute value of denominator (3) is larger than the numerator (2). +const negativeDenominator = isProperFraction(2, -3); +assertEquals(negativeDenominator, true); + +// Stretch 2: zero numerator +// Input: numerator = 0, denominator = 5 +// target output: true +// Explanation: The fraction 0/5 is a proper fraction because the absolute value of numerator (0) is less than the denominator (5). +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +// Stretch 3: zero denominator - this is mathematically undefined but we can decide how we want to handle it +// Input: numerator = 5, denominator = 0 +// target output: false +// Explanation: The fraction 5/0 is undefined in mathematics. In this implementation, we choose to return false. +const zeroDenominator = isProperFraction(5, 0); +assertEquals(zeroDenominator, false); + +// Stretch 4: both zero +// Input: numerator = 0, denominator = 0 +// target output: false +// Explanation: The fraction 0/0 is indeterminate in mathematics. In this implementation, we choose to return false. +const bothZero = isProperFraction(0, 0); +assertEquals(bothZero, false); + +// Stretch 5: negative numerator and denominator +// Input: numerator = -3, denominator = -5 +// target output: true +// Explanation: The fraction -3/-5 is a proper fraction because the absolute value of the numerator (3) is less than the absolute value of the denominator (5). The function should return true. +const negativeNumeratorAndDenominator = isProperFraction(-3, -5); +assertEquals(negativeNumeratorAndDenominator, true); + +// Stretch 6: improper negative numerator and denominator +// Input: numerator = -3, denominator = -2 +// target output: false +// Explanation: The fraction -3/-2 is an improper fraction because the absolute value of the numerator (3) is greater than the absolute value of the denominator (2). The function should return false. +const properNegativeNumeratorAndDenominator = isProperFraction(-3, -2); +assertEquals(properNegativeNumeratorAndDenominator, false); + +// Stretch 7: decimal values +// Input: numerator = 2.5, denominator = 3.5 +// target output: true +// Explanation: The fraction 2.5/3.5 is a proper fraction because the absolute value of the numerator (2.5) is less than the absolute value of the denominator (3.5). The function should return true. +const decimalValues = isProperFraction(2.5, 3.5); +assertEquals(decimalValues, true); + +// Stretch 8: improper decimal values +// Input: numerator = 3.5, denominator = 2.5 +// target output: false +// Explanation: The fraction 3.5/2.5 is an improper fraction because the absolute value of the numerator (3.5) is greater than the absolute value of the denominator (2.5). The function should return false. +const improperDecimalValues = isProperFraction(3.5, 2.5); +assertEquals(improperDecimalValues, false); + +// Stretch 9: invalid inputs (non-numeric values) +// Input: numerator = "a", denominator = 2 +// target output: false +// Explanation: The function should handle non-numeric inputs gracefully. In this case, we choose to return false. +const invalidInputs = isProperFraction("a", 2); +assertEquals(invalidInputs, 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 266525d1b..38f84cf2c 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 @@ -8,9 +8,23 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { + const errorMessage = "Error: Invalid card rank"; + + if (typeof card !== "string" || card.length < 2) { + return errorMessage; + } + const rank = card.slice(0, -1); if (rank === "A") { return 11; } + if (rank === "K" || rank === "Q" || rank === "J" || rank === "10") { + return 10; + } + const numericRank = Number(rank); + if (numericRank >= 2 && numericRank <= 9) { + return numericRank; + } + return errorMessage; } // The line below allows us to load the getCardValue function into tests in other files. @@ -31,27 +45,44 @@ function assertEquals(actualOutput, targetOutput) { // Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), // When the function getCardValue is called with this card string as input, // Then it should return the numerical card value -const aceofSpades = getCardValue("A♠"); -assertEquals(aceofSpades, 11); +const aceOfSpades = getCardValue("A♠"); +assertEquals(aceOfSpades, 11); // Handle Number Cards (2-10): // Given a card with a rank between "2" and "9", // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). -const fiveofHearts = getCardValue("5♥"); +const nineOfHearts = getCardValue("9♥"); // ====> write your test here, and then add a line to pass the test in the function above +assertEquals(nineOfHearts, 9); // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", // When the function is called with such a card, // Then it should return the value 10, as these cards are worth 10 points each in blackjack. +const kingOfDiamonds = getCardValue("K♦"); +assertEquals(kingOfDiamonds, 10); +const queenOfClubs = getCardValue("Q♣"); +assertEquals(queenOfClubs, 10); +const jackOfHearts = getCardValue("J♥"); +assertEquals(jackOfHearts, 10); +const tenOfSpades = getCardValue("10♠"); +assertEquals(tenOfSpades, 10); // Handle Ace (A): // Given a card with a rank of "A", // When the function is called with an Ace, // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. +const aceOfHearts = getCardValue("A♥"); +assertEquals(aceOfHearts, 11); // Handle Invalid Cards: // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." +const invalidCard = getCardValue("1♠"); +assertEquals(invalidCard, "Error: Invalid card rank"); +const anotherInvalidCard = getCardValue("Z♠"); +assertEquals(anotherInvalidCard, "Error: Invalid card rank"); +const emptyCard = getCardValue(""); +assertEquals(emptyCard, "Error: Invalid card rank"); 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 4a92a3e82..c47bec6fd 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 @@ -12,15 +12,103 @@ test("should identify right angle (90°)", () => { // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angle (<90°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); +}); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" +test("should identify obtuse angle (>90° and <180°)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle"); +}); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" +test("should identify straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" +test("should identify reflex angle (>180° and <360°)", () => { + expect(getAngleType(250)).toEqual("Reflex angle"); +}); + +// Case 6: Identify Full Rotation: +// When the angle is exactly 360 degrees, +// Then the function should return "Full rotation" +test("should identify full rotation (360°)", () => { + expect(getAngleType(360)).toEqual("Full rotation"); +}); + +// Case 7: input is a number in string +test("should return as usual when input is a number in string", () => { + expect(getAngleType("90")).toEqual("Right angle"); +}); + +// Case 8: input is not a number +test("should return 'Input should be a number or a number in string' when input is not a number", () => { + expect(getAngleType("hello")).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 9: input is less than 0 +test("should return 'Angle should be between 0 and 360' when input is less than 0", () => { + expect(getAngleType(-10)).toEqual("Angle should be between 0 and 360"); +}); + +// Case 10: input is greater than 360 +test("should return 'Angle should be between 0 and 360' when input is greater than 360", () => { + expect(getAngleType(400)).toEqual("Angle should be between 0 and 360"); +}); + +// Case 11: input is exactly 0 +test("should identify zero angle (0°)", () => { + expect(getAngleType(0)).toEqual("Zero angle"); +}); + +// Case 12: input is empty +test("should return 'Input should be a number or a number in string' when input is empty", () => { + expect(getAngleType()).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 13: input is null +test("should return 'Input should be a number or a number in string' when input is null", () => { + expect(getAngleType(null)).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 14: input is NaN +test("should return 'Input should be a number or a number in string' when input is NaN", () => { + expect(getAngleType(NaN)).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 15: input is undefined +test("should return 'Input should be a number or a number in string' when input is undefined", () => { + expect(getAngleType(undefined)).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 16: input is an object +test("should return 'Input should be a number or a number in string' when input is an object", () => { + expect(getAngleType({})).toEqual( + "Input should be a number or a number in string" + ); +}); + +// Case 17: input is an array +test("should return 'Input should be a number or a number in string' when input is an array", () => { + expect(getAngleType([])).toEqual( + "Input should be a number or a number in string" + ); +}); 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 caf08d15b..6e3a14c12 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 @@ -7,7 +7,130 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: +test("should return false for an improper fraction", () => { + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for a negative proper fraction", () => { + expect(isProperFraction(-4, 7)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); + +// Stretch 1: Identify Negative Denominator: +test("should return true when 2 < |3|", () => { + expect(isProperFraction(2, -3)).toEqual(true); +}); + +// Stretch 2: Identify Zero Numerator: +test("should return true when 0 < 5", () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); + +// Stretch 3: Handle Zero Denominator: +// This test checks how the function handles a zero denominator, which is mathematically undefined. +// We expect the function to return false in this case. +test("should return false when 3 > 0", () => { + expect(isProperFraction(3, 0)).toEqual(false); +}); + +// Stretch 4: Handle Both Numerator and Denominator as Zero: +// This test checks how the function handles both numerator and denominator being zero. +// We expect the function to return false in this case as well. +test("should return false when both numerator and denominator are zero", () => { + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// Stretch 5: Identify Negative Numerator and Negative Denominator: +test("should return true when |3| < |5|", () => { + expect(isProperFraction(-3, -5)).toEqual(true); +}); + +// Stretch 6: Identify Improper Negative Numerator and Negative Denominator: +test("should return false when |3| > |2|", () => { + expect(isProperFraction(-3, -2)).toEqual(false); +}); + +// Stretch 7: Identify Decimal Values: +test("should return true when 2.5 < 3.5", () => { + expect(isProperFraction(2.5, 3.5)).toEqual(true); +}); + +// Stretch 8: Identify Improper Decimal Values: +test("should return false when 3.5 > 2.5", () => { + expect(isProperFraction(3.5, 2.5)).toEqual(false); +}); + +// Stretch 9: Identify invalid Inputs (Non-numeric Values): +test("should return false for non-numeric inputs", () => { + expect(isProperFraction("a", 2)).toEqual(false); +}); + +// Stretch 10: numerator is a string that can be converted to a number +test("should return true when '2' < 3", () => { + expect(isProperFraction("2", 3)).toEqual(true); +}); + +// Stretch 11: denominator is a string that can be converted to a number +test("should return false when 5 > '2'", () => { + expect(isProperFraction(5, "2")).toEqual(false); +}); + +// Stretch 12: both numerator and denominator are strings that can be converted to numbers +test("should return true when '2' < '3'", () => { + expect(isProperFraction("2", "3")).toEqual(true); +}); + +// Stretch 13: numerator is null +test("should return false when numerator is null", () => { + expect(isProperFraction(null, 3)).toEqual(false); +}); + +// Stretch 14: denominator is null +test("should return false when denominator is null", () => { + expect(isProperFraction(3, null)).toEqual(false); +}); + +// Stretch 15: both numerator and denominator are null +test("should return false when both numerator and denominator are null", () => { + expect(isProperFraction(null, null)).toEqual(false); +}); + +// Stretch 16: numerator is undefined +test("should return false when numerator is undefined", () => { + expect(isProperFraction(undefined, 3)).toEqual(false); +}); + +// Stretch 17: denominator is undefined +test("should return false when denominator is undefined", () => { + expect(isProperFraction(3, undefined)).toEqual(false); +}); + +// Stretch 18: both numerator and denominator are undefined +test("should return false when both numerator and denominator are undefined", () => { + expect(isProperFraction(undefined, undefined)).toEqual(false); +}); + +// Stretch 19: numerator is an array +test("should return false when numerator is an array", () => { + expect(isProperFraction([2], 3)).toEqual(false); +}); + +// Stretch 20: denominator is an array +test("should return false when denominator is an array", () => { + expect(isProperFraction(2, [3])).toEqual(false); +}); + +// Stretch 21: numerator is an object +test("should return false when numerator is an object", () => { + expect(isProperFraction({ num: 2 }, 3)).toEqual(false); +}); + +// Stretch 22: denominator is an object +test("should return false when denominator is an object", () => { + expect(isProperFraction(2, { den: 3 })).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 04418ff72..96e25097e 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 @@ -3,11 +3,81 @@ const getCardValue = require("../implement/3-get-card-value"); test("should return 11 for Ace of Spades", () => { - const aceofSpades = getCardValue("A♠"); - expect(aceofSpades).toEqual(11); + const aceOfSpades = getCardValue("A♠"); + expect(aceOfSpades).toEqual(11); }); // Case 2: Handle Number Cards (2-10): +test("should return 9 for Nine of Hearts", () => { + const nineOfHearts = getCardValue("9♥"); + expect(nineOfHearts).toEqual(9); +}); +test("should return 5 for Five of Hearts", () => { + const fiveOfHearts = getCardValue("5♥"); + expect(fiveOfHearts).toEqual(5); +}); +test("should return 2 for Two of Diamonds", () => { + const twoOfDiamonds = getCardValue("2♦"); + expect(twoOfDiamonds).toEqual(2); +}); // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for King of Diamonds", () => { + const kingOfDiamonds = getCardValue("K♦"); + expect(kingOfDiamonds).toEqual(10); +}); +test("should return 10 for Queen of Clubs", () => { + const queenOfClubs = getCardValue("Q♣"); + expect(queenOfClubs).toEqual(10); +}); +test("should return 10 for Jack of Hearts", () => { + const jackOfHearts = getCardValue("J♥"); + expect(jackOfHearts).toEqual(10); +}); +test("should return 10 for Ten of Spades", () => { + const tenOfSpades = getCardValue("10♠"); + expect(tenOfSpades).toEqual(10); +}); // Case 4: Handle Ace (A): +test("should return 11 for Ace of Hearts", () => { + const aceOfHearts = getCardValue("A♥"); + expect(aceOfHearts).toEqual(11); +}); // Case 5: Handle Invalid Cards: +const errorMessage = "Error: Invalid card rank"; + +test("should throw an error for invalid card rank", () => { + const invalidCard = getCardValue("1♠"); + expect(invalidCard).toEqual(errorMessage); +}); +test("should throw an error for another invalid card rank", () => { + const anotherInvalidCard = getCardValue("Z♠"); + expect(anotherInvalidCard).toEqual(errorMessage); +}); +test("should throw an error for empty card string", () => { + const emptyCard = getCardValue(""); + expect(emptyCard).toEqual(errorMessage); +}); +test("should throw an error for boolean (true) input", () => { + const booleanCard = getCardValue(true); + expect(booleanCard).toEqual(errorMessage); +}); +test("should throw an error for boolean (false) input", () => { + const booleanCard = getCardValue(false); + expect(booleanCard).toEqual(errorMessage); +}); +test("should throw an error for numeric input", () => { + const numericCard = getCardValue(10); + expect(numericCard).toEqual(errorMessage); +}); +test("should throw an error for null input", () => { + const nullCard = getCardValue(null); + expect(nullCard).toEqual(errorMessage); +}); +test("should throw an error for undefined input", () => { + const undefinedCard = getCardValue(undefined); + expect(undefinedCard).toEqual(errorMessage); +}); +test("should throw an error for object input", () => { + const objectCard = getCardValue({ rank: "A", suit: "♠" }); + expect(objectCard).toEqual(errorMessage); +});