diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..2f622599e --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 2f622599e09fc28147949f59bcb6892ff6721548 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..26da0a0ea 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,15 +15,29 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + 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. 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 +// Helper function for testing function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, @@ -31,7 +45,28 @@ function assertEquals(actualOutput, targetOutput) { ); } -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles +// Tests const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(135); +assertEquals(obtuse, "Obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +const invalid = getAngleType(-45); +assertEquals(invalid, "Invalid angle"); +assertEquals(getAngleType(0), "Invalid angle"); +assertEquals(getAngleType(89), "Acute angle"); +assertEquals(getAngleType(91), "Obtuse angle"); +assertEquals(getAngleType(179), "Obtuse angle"); +assertEquals(getAngleType(181), "Reflex angle"); +assertEquals(getAngleType(359), "Reflex angle"); +assertEquals(getAngleType(360), "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..972ea4472 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,9 +11,21 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0) { + return false; // A fraction with a zero denominator is undefined, so we return false. + } + + if (numerator === 0) { + return true; // A fraction with a zero numerator is a proper fraction (0/denominator). + } + if (numerator < 0 && denominator > 0) { + return false; + // Negative numerator, positive denominator + } + return Math.abs(numerator) < Math.abs(denominator); + + return numerator < denominator && numerator > 0 && denominator > 0; } - // 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; @@ -31,3 +43,9 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); +assertEquals(isProperFraction(-1, -2), true); +assertEquals(isProperFraction(0, 2), true); +assertEquals(isProperFraction(2, 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..6881b14e8 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,49 @@ // 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 = ["♠", "♥", "♦", "♣"]; + + if (typeof card !== "string") { + throw new Error("Invalid card"); + } + + if (card.length < 2) { + throw new Error("Invalid card"); + } + + const rank = card.substring(0, card.length - 1); + const suit = card.substring(card.length - 1); + + if (!validRanks.includes(rank)) { + throw new Error("Invalid card"); + } + + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + + if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + return parseInt(rank); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,7 +82,10 @@ 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("J♦"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♦"), 10); // Handling invalid cards try { getCardValue("invalid"); @@ -50,3 +95,22 @@ try { } catch (e) {} // What other invalid card cases can you think of? +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("J♥"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♠"), 10); + +try { + getCardValue("A"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("invalid"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("2♠♣"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} 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..49df7ee07 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,9 +2,6 @@ // 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 @@ -14,7 +11,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when angle is exactly 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(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is exactly 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(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when angle is negative`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); +}); +test(`should return "Invalid angle" when angle is 360 or greater`, () => { + expect(getAngleType(360)).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..23776762c 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,31 @@ 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 +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Case: numerator is zero +test(`should return true when numerator is zero and denominator is positive`, () => { + expect(isProperFraction(0, 1)).toEqual(true); +}); +test("should return true when numerator is smaller than denominator and both are positive", () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +test("should return false when numerator is equal to denominator", () => { + expect(isProperFraction(2, 2)).toEqual(false); +}); + +test("should return false when numerator is greater than denominator", () => { + expect(isProperFraction(3, 2)).toEqual(false); +}); + +test("should return false when numerator is negative", () => { + expect(isProperFraction(-1, 2)).toEqual(false); +}); + +test("should return true when both numerator and denominator are negative and numerator is smaller than denominator", () => { + expect(isProperFraction(-1, -2)).toEqual(true); +});