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..cc40fb853 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,14 +8,28 @@ // - "Reflex angle" for angles greater than 180° and less than 360° // - "Invalid angle" for angles outside the valid range. +const { assertEquals, isProperFraction } = require("./2-is-proper-fraction"); + // Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) // Acceptance criteria: // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. -function getAngleType(angle) { +function getAngleType(angle) { // This function should return a string indicating the type of angle based on the input angle in degrees. // 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. @@ -35,3 +49,34 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); + +const obtuse = getAngleType(120); +assertEquals(obtuse, "obtuse angle"); + +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); + +const relex = getAngleType(270); +assertEquals(relex, "Reflex angle"); + +const invalid = getAngleType(400); +assertEquals(invalid, "Invalid angle"); +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(0, 0), false); +// Case 3: Obtuse angles +test(`should return "obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(120)).toEqual("obtuse angle"); + expect(getAngleType(179)).toEqual("obtuse angle"); + expect(getAngleType(179.99)).toEqual("obtuse angle"); + expect(getAngleType(179.999)).toEqual("obtuse angle"); + expect(getAngleType(179.9999)).toEqual("obtuse angle"); + expect(getAngleType(179.99999)).toEqual("obtuse angle"); + expect(getAngleType(179.999999)).toEqual("obtuse angle"); + expect(getAngleType(179.9999999)).toEqual("obtuse angle"); + expect(getAngleType(179.99999999)).toEqual("obtuse angle"); + expect(getAngleType(179.999999999)).toEqual("obtuse 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..7a6425bc1 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 @@ -12,7 +12,14 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator === 0){ + return false; + } + + return Math.abs(numerator) < Math.abs(denominator); + } +exports.isProperFraction = isProperFraction; // 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. @@ -25,9 +32,17 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } +exports.assertEquals = assertEquals; // 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(1, 2), true); // 2 is greater than 1, so this should return true. +assertEquals(isProperFraction(2, 1), false); // 1 is not greater than 2, so this should return false. +assertEquals(isProperFraction(0, 1), true); +assertEquals(isProperFraction(1, 0), false); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), false); +assertEquals(isProperFraction(-1, -2), 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..a5d4dfd2f 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 @@ -23,8 +23,35 @@ function getCardValue(card) { // TODO: Implement this function + const rank = card.slice(0, -1); // get the rank by slicing off the last character (the suit) + const suit = card.slice(-1); // get the suit by taking the last character + + const validSuits = ["♠", "♥", "♦", "♣"]; // Define valid suits + if (!validSuits.includes(suit)) { // check if the suit is valid + throw new Error("Invalid card"); // If the suit is not valid, throw an error + } + + const faceCardValues = { // Define the values for face cards and ace + "A": 11, // Ace is worth 11 + "J": 10, // Jack is worth 10 + "Q": 10, // Queen is worth 10 + "K": 10 // King is worth 10 + }; + if (faceCardValues[rank]) { // Check if the rank is a face card or ace + return faceCardValues[rank]; // If it is, return the corresponding value + } + + const numericValue = parseInt(rank); // Try to parse the rank as a number + if (numericValue >= 2 && numericValue <= 10) { // check if the numeric value is between 2 and 10 + return numericValue; // If it is, return the numeric value + } + + throw new Error("Invalid card"); // If the rank is not valid (not a face card, ace, or number between 2 and 10), throw an error } +console.log(getCardValue("2♥")); // 2 +console.log(getCardValue("J♣")); // 10 + // 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; @@ -39,14 +66,31 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: -assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("9♠"), 9); // should return 9 for the 9 of spades +assertEquals(getCardValue("A♥"), 11); // should return 11 for the ace of hearts +assertEquals(getCardValue("J♣"), 10); // should return 10 for the jack of clubs +assertEquals(getCardValue("Q♦"), 10); // should return 10 for the queen of diamonds +assertEquals(getCardValue("K♠"), 10); // should return 10 for the king of spades +assertEquals(getCardValue("10♥"), 10); // should return 10 for the 10 of hearts +assertEquals(getCardValue("2♦"), 2); // should return 2 for the 2 of diamonds + // Handling invalid cards -try { - getCardValue("invalid"); +try { + getCardValue("1 ♠"); // Invalid rank // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); } catch (e) {} + // What other invalid card cases can you think of? +try { + getCardValue("11♠"); // Invalid rank + console.error("Error was not thrown for invalid card"); +} catch (e) {} + +try { + getCardValue("h♠"); // Valid card + console.error("Error was thrown for valid 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..0ba23c974 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 @@ -8,13 +8,42 @@ const getAngleType = require("../implement/1-get-angle-type"); // Case 1: Acute angles 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(1)).toEqual("Acute angle"); // should return "Acute angle" for angles just above 0 + expect(getAngleType(45)).toEqual("Acute angle"); // should return "Acute angle" for a typical acute angle + expect(getAngleType(89)).toEqual("Acute angle"); // should return "Acute angle" for angles just below 90 }); // Case 2: Right angle -// Case 3: Obtuse angles +test('should return "Right angle" when angle is exactly 90', () => { + expect(getAngleType(90)).toEqual("Right 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(190)).toEqual("Reflex angle"); + expect(getAngleType(200)).toEqual("Reflex angle"); + expect(getAngleType(300)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); + expect(getAngleType(359.90)).toEqual("Reflex angle"); + expect(getAngleType(359.99)).toEqual("Reflex angle"); + expect(getAngleType(359.999)).toEqual("Reflex angle"); + expect(getAngleType(359.9999)).toEqual("Reflex angle"); + expect(getAngleType(359.99999)).toEqual("Reflex angle"); + expect(getAngleType(359.999999)).toEqual("Reflex angle"); + expect(getAngleType(359.9999999)).toEqual("Reflex angle"); + expect(getAngleType(359.99999999)).toEqual("Reflex angle"); + expect(getAngleType(359.999999999)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid angle" when angle (angle <= 0 or angle >= 360)`, () => { + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-0.1)).toEqual("Invalid angle"); + expect(getAngleType(-10)).toEqual("Invalid angle"); + 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..b92e72dad 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 @@ -8,3 +8,45 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +test(`should return true in normal cases where numerator < denominator`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +test(`should return false when numerator >= denominator`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(2, 2)).toEqual(false); +}); + +test('should return true when numerator is negative and denominator is positive', () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(-2, 1)).toEqual(true); + expect(isProperFraction(-2, 2)).toEqual(true) + +}); + +test(`should return false when numerator is positive and denominator is negative`, () => { + expect(isProperFraction(1, -2)).toEqual(false); + expect(isProperFraction(2, -1)).toEqual(false); + +}); + +test(`should return false when numerator and denominator are both negative`, () => { + expect(isProperFraction(-1, -2)).toEqual(false); + expect(isProperFraction(-2, -1)).toEqual(false); + expect(isProperFraction(-2, -2)).toEqual(false); +}); + +test(`should return true when numerator is zero and denominator is positive`, () => { + expect(isProperFraction(0, 1)).toEqual(true); + expect(isProperFraction(0, 2)).toEqual(true); + +}); + +test(`should return false when numerator is zero and denominator is negative`, () => { + expect(isProperFraction(0, -1)).toEqual(false); + expect(isProperFraction(0, -2)).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..f93f82d4d 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 @@ -9,10 +9,33 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test(`Should return the correct value for number cards (2-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 for face cards (J, Q, K)`, () => { + expect(getCardValue("J♥")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); +}); + // Invalid Cards +test(`Should throw an error for invalid cards`, () => { + expect(() => getCardValue("1♠")).toThrow("Invalid card"); + expect(() => getCardValue("11♣")).toThrow("Invalid card"); + expect(() => getCardValue("A♦")).toThrow("Invalid card"); +}); // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: