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..1f3383ff62 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 < 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. @@ -25,13 +37,44 @@ 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 function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` + ); } // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const invalid = getAngleType(0); +assertEquals(invalid, "Invalid angle"); + +const acute = getAngleType(89); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(91); +assertEquals(obtuse, "Obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle") + +const invalid1 = getAngleType(360); +assertEquals(invalid1, "Invalid angle") + +const acute1 = getAngleType(1); +assertEquals(acute1, "Acute angle") + +const obtuse1 = getAngleType(179); +assertEquals(obtuse1, "Obtuse angle") + +const reflex = getAngleType(181); +assertEquals(reflex, "Reflex angle") + +const reflex1 = getAngleType(359); +assertEquals(reflex1, "Reflex angle") + +const invalid2 = getAngleType(-1); +assertEquals(invalid2, "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..9084237fa4 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,11 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (Number.isFinite(numerator) && Number.isFinite(denominator)) { + if(numerator < denominator){ + return true + } + } return false } // The line below allows us to load the isProperFraction function into tests in other files. @@ -20,14 +24,36 @@ module.exports = isProperFraction; // Here's our helper again function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` + ); } -// TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(0, 5), true); +assertEquals(isProperFraction(5, 0), false); +assertEquals(isProperFraction(0, 0), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), false); +assertEquals(isProperFraction(-2, -1), true); +assertEquals(isProperFraction(-1, -2), false); +assertEquals(isProperFraction(0.5, 1), true); +assertEquals(isProperFraction(1.5, 1), false); +assertEquals(isProperFraction(Infinity, 2), false); +assertEquals(isProperFraction(2, Infinity), false); +assertEquals(isProperFraction(NaN, 2), false); +assertEquals(isProperFraction(2, NaN), false); +assertEquals(isProperFraction(999999999, 1000000000), true); + + + + + + + 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..94b781830f 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,33 +22,104 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + // Basic validation: validating the input before slicing + if (card === "") { + throw new Error("No card was played") + } + if (card.length < 2 || card.length > 3) { + throw new Error("Invalid card played, rank and suit cannot be less than 1 or more than 3") + } + const rank = card.slice(0, card.length - 1).toUpperCase() + const suit = card.slice(card.length - 1) + + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + + // Suit and rank validation + if (!validSuits.includes(suit)) { + throw new Error("Invalid card played, suit is missing"); + } + if (validRanks.includes(rank)) { + if (rank === "A") { + return 11; + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else { + return Number(rank); + } + } else { + throw new Error("Invalid 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. function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${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("A♥"), 11); +assertEquals(getCardValue("A♦"), 11); +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("3♥"), 3); +assertEquals(getCardValue("4♦"), 4); +assertEquals(getCardValue("5♣"), 5); +assertEquals(getCardValue("6♠"), 6); +assertEquals(getCardValue("7♥"), 7); +assertEquals(getCardValue("8♦"), 8); +assertEquals(getCardValue("9♣"), 9); +assertEquals(getCardValue("10♠"), 10); +assertEquals(getCardValue("J♠"), 10); +assertEquals(getCardValue("Q♥"), 10); +assertEquals(getCardValue("K♦"), 10); // Handling invalid cards try { - getCardValue("invalid"); + getCardValue(""); + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log(e); +} + +// What other invalid card cases can you think of? +try { + getCardValue("100"); + console.error("Error was not thrown for card with more than 3 in length"); +} catch (e) { + console.log(e) +} +try { + getCardValue("1") + console.error("Error was not thrown for a card.lenght = 1") +} catch (e) { + console.log(e) +} + +try { + getCardValue("♦") + console.error("Error was not thrown for a card play of just suits") +} catch (e) { + console.log(e) +} - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card 😢"); +try { + getCardValue("A😊") + console.error("Error was not thrown for a card play of a wrong suit") } catch (e) { - console.log("Error thrown for invalid card 🎉"); + console.log(e) } -// What other invalid card cases can you think of? + 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..4e6978d729 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,7 +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 @@ -13,8 +12,30 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { expect(getAngleType(89)).toEqual("Acute angle"); }); -// Case 2: Right angle -// Case 3: Obtuse angles + +// Case 2: Obtuse angle +test(`should return "obtuse angle" when (90 < angle < 180 `, () =>{ + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); + +}) +// Case 3: Right angles +test(`should return "Right angle" when (angle === 90)`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}) + // Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("Straight angle") +}) // Case 5: Reflex angles +test(`should return "Reflect 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 (0 > angle > 360)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-1)).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..49b2e58352 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,36 @@ // 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. -// Special case: numerator is zero -test(`should return false when denominator is zero`, () => { - expect(isProperFraction(1, 0)).toEqual(false); +test("should correctly identify proper fractions", () => { + // Whole number fractions + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(5, 5)).toEqual(false); + + // Zero + expect(isProperFraction(0, 5)).toEqual(true); + expect(isProperFraction(5, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); + + // Negative numbers + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).toEqual(false); + expect(isProperFraction(-2, -1)).toEqual(true); + expect(isProperFraction(-1, -2)).toEqual(false); + + // Decimal numbers + expect(isProperFraction(0.5, 1)).toEqual(true); + expect(isProperFraction(1.5, 1)).toEqual(false); + + // Infinity + expect(isProperFraction(Infinity, 2)).toEqual(false); + expect(isProperFraction(2, Infinity)).toEqual(false); + + // NaN + expect(isProperFraction(NaN, 2)).toEqual(false); + expect(isProperFraction(2, NaN)).toEqual(false); + + // Large numbers + expect(isProperFraction(999999999, 1000000000)).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..2ab7e47d80 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,17 +2,42 @@ // 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: +// Suggestion: Group the remaining test data into these categories: // ♠ ♥ ♦ ♣ // Number Cards (2-10) +test(`Should return the card's numeric rank for cards 2 through 10`, () =>{ + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("3♥")).toEqual(3); + expect(getCardValue("4♦")).toEqual(4); + expect(getCardValue("5♣")).toEqual(5); + expect(getCardValue("6♠")).toEqual(6); + expect(getCardValue("7♥")).toEqual(7); + expect(getCardValue("8♦")).toEqual(8); + expect(getCardValue("9♣")).toEqual(9); + expect(getCardValue("10♠")).toEqual(10); + +}) // Face Cards (J, Q, K) +test(`should return 10 When the card is a face card ("J", "Q", "K")`, () => { + expect(getCardValue("j♠")).toEqual(10); + expect(getCardValue("k♦")).toEqual(10); + expect(getCardValue("q♣")).toEqual(10); + +}) // Invalid Cards +test("should throw an error when an invalid card is played", () => { + expect(() => getCardValue("")).toThrow("No card was played"); + expect(() => getCardValue("1009♣")).toThrow("Invalid card played, rank and suit cannot be less " + + "than 1 or more than 3") + +}); + + // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: