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..6724c70ff 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,12 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle > 0 && angle < 90) return "Acute angle"; + if (angle === 90) return "Right angle"; + if (angle > 90 && angle < 180) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + 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. @@ -35,3 +40,18 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const acute = getAngleType(47); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(139); +assertEquals(obtuse, "Obtuse angle"); + +const reflex = getAngleType(258); +assertEquals(reflex, "Reflex angle"); + +const invalid = getAngleType(360); +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..dd516b9e7 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,13 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + // console.log(numerator, denominator); + // console.log(Math.abs(numerator), Math.abs(denominator)); + if (denominator !== 0 && 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. @@ -31,3 +37,21 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +assertEquals(isProperFraction(9, 10), true); + +assertEquals(isProperFraction(-6, 8), true); + +assertEquals(isProperFraction(-2, -3), true); + +assertEquals(isProperFraction(3.5, 6), true); + +assertEquals(isProperFraction(4, 4.67), true); + +assertEquals(isProperFraction(7, 0), false); + +assertEquals(isProperFraction(2, 1), false); + +assertEquals(isProperFraction(-9, -3), false); + +assertEquals(isProperFraction(-5.68, 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..231fd0a30 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,43 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const validSuits = ["♠", "♥", "♦", "♣"]; + if (typeof card !== "string") + throw new Error(`Invalid card format: "${card}". Expected a string.`); + + let cardSuit = card.slice(-1); + let cardRank = card.slice(0, -1); + + if (validSuits.includes(cardSuit)) { + switch (cardRank) { + case "A": + return 11; + + case "J": + case "Q": + case "K": + return 10; + + case "2": + case "3": + case "4": + case "5": + case "6": + case "7": + case "8": + case "9": + case "10": + return Number(cardRank); + + default: + throw new Error( + `Invalid card format: "${card}" has an invalid rank "${cardRank}". Expected one of: A, 2–10, J, Q, K.` + ); + } + } else + throw new Error( + `Invalid card format: "${card}" has an invalid suit "${cardSuit}". Expected one of: ♠, ♥, ♦, ♣.` + ); } // The line below allows us to load the getCardValue function into tests in other files. @@ -41,12 +77,147 @@ function assertEquals(actualOutput, targetOutput) { // Examples: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("4♣"), 4); + +assertEquals(getCardValue("A♥"), 11); + +assertEquals(getCardValue("J♦"), 10); + +assertEquals(getCardValue("Q♣"), 10); + +assertEquals(getCardValue("K♠"), 10); + +assertEquals(getCardValue("K♦"), 10); + +assertEquals(getCardValue("10♥"), 10); + +assertEquals(getCardValue("2♦"), 2); + // 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) {} +} catch (err) { + console.error(err); +} // What other invalid card cases can you think of? + +try { + getCardValue(""); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("1♥"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("11♠"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("0♦"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("4.8♣"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("G♦"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("9"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("K"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("9M"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("♠3"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue(6); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("A♦!"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} + +try { + getCardValue("♥"); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card"); +} catch (err) { + console.error(err); +} 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..fc508f6f6 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 @@ -10,11 +10,42 @@ 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"); + expect(getAngleType(89.436)).toEqual("Acute angle"); }); // 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)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(92)).toEqual("Obtuse angle"); + expect(getAngleType(111.1)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle = 180)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(294)).toEqual("Reflex angle"); + expect(getAngleType(357.34)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle <= 0 || angle >= 360)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-90)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(411.65)).toEqual("Invalid angle"); + expect(getAngleType(256578)).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..46fa8c75f 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 @@ -5,6 +5,45 @@ 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`, () => { - expect(isProperFraction(1, 0)).toEqual(false); + +test(`should return true when the absolute value of the numerator is less than the absolute value of the denominator and the denominator is not 0`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(1, -3)).toEqual(true); + expect(isProperFraction(3.5, 4)).toEqual(true); + expect(isProperFraction(-99, -100)).toEqual(true); + expect(isProperFraction(8, 12.5)).toEqual(true); + expect(isProperFraction(-6, 12)).toEqual(true); + expect(isProperFraction(0.03, 0.1)).toEqual(true); +}); + +test(`should return true when numerator is equal to 0 and denominator is either positive or negative`, () => { + expect(isProperFraction(0, -5)).toEqual(true); + expect(isProperFraction(0, 56)).toEqual(true); + expect(isProperFraction(0, 0.1)).toEqual(true); + expect(isProperFraction(0, -8.9)).toEqual(true); +}); + +test(`should return false when the denominator is equal to zero`, () => { + expect(isProperFraction(-1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); + expect(isProperFraction(15, 0)).toEqual(false); + expect(isProperFraction(6.4, 0)).toEqual(false); + expect(isProperFraction(-8.5, 0)).toEqual(false); +}); + +test(`should return false when the absolute value of the numerator is greater than the absolute value of the denominator`, () => { + expect(isProperFraction(3, 2)).toEqual(false); + expect(isProperFraction(-9, 3)).toEqual(false); + expect(isProperFraction(-15, -4)).toEqual(false); + expect(isProperFraction(36, -5)).toEqual(false); + expect(isProperFraction(3.8, 2.7)).toEqual(false); + expect(isProperFraction(1, 0.7)).toEqual(false); +}); + +test(`should return false when the absolute value of the numerator is equal to the absolute value of the denominator`, () => { + expect(isProperFraction(3, 3)).toEqual(false); + expect(isProperFraction(-19, -19)).toEqual(false); + expect(isProperFraction(7.45, 7.45)).toEqual(false); + expect(isProperFraction(67, -67)).toEqual(false); + expect(isProperFraction(-8, 8)).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..3c57167d7 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 @@ -7,14 +7,46 @@ const getCardValue = require("../implement/3-get-card-value"); // Case 1: Ace (A) test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♦")).toEqual(11); + expect(getCardValue("A♥")).toEqual(11); + expect(getCardValue("A♣")).toEqual(11); }); // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return the number value of a card when given an a number card`, () => { + expect(getCardValue("7♠")).toEqual(7); + expect(getCardValue("10♠")).toEqual(10); + expect(getCardValue("2♦")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("10♣")).toEqual(10); +}); + // Face Cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + expect(getCardValue("J♦")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); + expect(getCardValue("J♥")).toEqual(10); + expect(getCardValue("Q♣")).toEqual(10); +}); + // Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("")).toThrow(/Invalid card format/); + expect(() => getCardValue("invalid")).toThrow(/Invalid card format/); + expect(() => getCardValue("11♠")).toThrow(/Invalid card format/); + expect(() => getCardValue("0♦")).toThrow(/Invalid card format/); + expect(() => getCardValue("4.8♣")).toThrow(/Invalid card format/); + expect(() => getCardValue("G♦")).toThrow(/Invalid card format/); + expect(() => getCardValue("9")).toThrow(/Invalid card format/); + expect(() => getCardValue("K")).toThrow(/Invalid card format/); + expect(() => getCardValue(6)).toThrow(/Invalid card format/); + expect(() => getCardValue("♠3")).toThrow(/Invalid card format/); + expect(() => getCardValue("9M")).toThrow(/Invalid card format/); +}); // 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 -