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..3b342faec 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 @@ -16,6 +16,22 @@ function getAngleType(angle) { // TODO: Implement this function + if ( angle <=0 || angle >= 360 ) { + return "Invalid angle"; + } + if ( 0 < angle && angle < 90 ){ + return "Acute angle"; + } + if (angle === 90){ + return "Right angle"; + } + if ( 90 < angle && angle < 180){ + return "Obtuse angle"; + } + if (angle === 180){ + return "Straight angle"; + } + return "Reflex angle"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +51,13 @@ 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 reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); +const zero = getAngleType(0); +assertEquals(zero, "Invalid angle"); \ No newline at end of file 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..c309e21a3 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,7 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return numerator > 0 && denominator > 0 && numerator < denominator; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +31,26 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// Proper fractions - should return true +assertEquals(isProperFraction(3, 4), true); +assertEquals(isProperFraction(1, 5), true); +assertEquals(isProperFraction(2, 3), true); +assertEquals(isProperFraction(5, 8), true); +assertEquals(isProperFraction(1, 100), true); + +// Improper fractions - should return false +assertEquals(isProperFraction(2, 1), false); +assertEquals(isProperFraction(5, 3), false); +assertEquals(isProperFraction(4, 4), false); +assertEquals(isProperFraction(10, 5), false); +assertEquals(isProperFraction(100, 99), false); + +// Edge cases with zero - should return false +assertEquals(isProperFraction(0, 5), false); +assertEquals(isProperFraction(0, 1), false); + +// Negative numbers - should return false +assertEquals(isProperFraction(-1, 2), false); +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..e24045042 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,39 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + // Define valid ranks and suits + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + const validSuits = ["♠", "♥", "♦", "♣"]; + + // Check if card is a valid string + if (typeof card !== 'string' || card.length === 0) { + throw new Error('Invalid card'); + } + + // Extract suit (last character) + const suit = card[card.length - 1]; + + // Extract rank (everything except last character) + const rank = card.slice(0, -1); + + // Validate suit + if (!validSuits.includes(suit)) { + throw new Error('Invalid card'); + } + + // Validate rank + if (!validRanks.includes(rank)) { + throw new Error('Invalid card'); + } + + // Return value based on rank + 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. @@ -38,15 +70,88 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: + +// Test all Ace cards (should return 11) +assertEquals(getCardValue("A♠"), 11); +assertEquals(getCardValue("A♥"), 11); +assertEquals(getCardValue("A♦"), 11); +assertEquals(getCardValue("A♣"), 11); + +// Test all face cards (should return 10) +assertEquals(getCardValue("J♠"), 10); +assertEquals(getCardValue("J♥"), 10); +assertEquals(getCardValue("J♦"), 10); +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("Q♥"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♠"), 10); +assertEquals(getCardValue("K♥"), 10); +assertEquals(getCardValue("K♦"), 10); +assertEquals(getCardValue("K♣"), 10); + +// Test number cards (should return their numeric value) +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); -// Handling invalid cards +// Handling invalid cards - generic invalid string 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) {} -// What other invalid card cases can you think of? +// Card without suit +try { + getCardValue("A"); + console.error("Error was not thrown for card without suit"); +} catch (e) {} + +// Card without rank (just a suit) +try { + getCardValue("♠"); + console.error("Error was not thrown for card without rank"); +} catch (e) {} + +// Invalid rank +try { + getCardValue("11♠"); + console.error("Error was not thrown for invalid rank '11'"); +} catch (e) {} + +try { + getCardValue("X♠"); + console.error("Error was not thrown for invalid rank 'X'"); +} catch (e) {} + +// Invalid suit +try { + getCardValue("A♪"); + console.error("Error was not thrown for invalid suit"); +} catch (e) {} + +try { + getCardValue("AH"); + console.error("Error was not thrown for invalid suit 'H'"); +} catch (e) {} + +// Empty string +try { + getCardValue(""); + console.error("Error was not thrown for empty string"); +} catch (e) {} + +// Extra characters +try { + getCardValue("A♠X"); + console.error("Error was not thrown for extra characters"); +} catch (e) {} + +console.log("All tests completed!"); 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..b17ca9a80 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 @@ -14,7 +14,38 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // 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 obtuse angles, including boundary cases + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse 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 "Reflex angle" when (180 < angle < 360)`, () => { + // Test various reflex angles, including boundary cases + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" for angles outside valid range`, () => { + // Test boundary and invalid cases + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(-90)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid angle"); + expect(getAngleType(1000)).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..461919246 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,42 @@ 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 +// Proper fractions - should return true +test(`should return true for proper fractions (0 < numerator < denominator)`, () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 4)).toEqual(true); + expect(isProperFraction(1, 5)).toEqual(true); + expect(isProperFraction(2, 3)).toEqual(true); + expect(isProperFraction(5, 8)).toEqual(true); + expect(isProperFraction(1, 100)).toEqual(true); +}); + +// Improper fractions - should return false +test(`should return false for improper fractions (numerator >= denominator)`, () => { + expect(isProperFraction(2, 1)).toEqual(false); + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(4, 4)).toEqual(false); + expect(isProperFraction(10, 5)).toEqual(false); + expect(isProperFraction(100, 99)).toEqual(false); +}); + +// Edge cases with zero - should return false +test(`should return false when numerator is zero`, () => { + expect(isProperFraction(0, 5)).toEqual(false); + expect(isProperFraction(0, 1)).toEqual(false); + expect(isProperFraction(0, 100)).toEqual(false); +}); + +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(5, 0)).toEqual(false); +}); + +// Negative numbers - should return false +test(`should return false for negative numbers`, () => { + expect(isProperFraction(-1, 2)).toEqual(false); + expect(isProperFraction(1, -2)).toEqual(false); + expect(isProperFraction(-1, -2)).toEqual(false); + expect(isProperFraction(-5, -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 cf7f9dae2..85b29f95e 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,19 +2,103 @@ // 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`, () => { +// Case 1: Ace (A) - Should return 11 +test(`Should return 11 when given an ace card (A♠)`, () => { expect(getCardValue("A♠")).toEqual(11); }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards +test(`Should return 11 when given an ace card (A♥)`, () => { + expect(getCardValue("A♥")).toEqual(11); +}); + +test(`Should return 11 when given an ace card (A♦)`, () => { + expect(getCardValue("A♦")).toEqual(11); +}); + +test(`Should return 11 when given an ace card (A♣)`, () => { + expect(getCardValue("A♣")).toEqual(11); +}); + +// Case 2: Number Cards (2-10) +test(`Should return 2 when given a number card (2♠)`, () => { + expect(getCardValue("2♠")).toEqual(2); +}); + +test(`Should return 3 when given a number card (3♥)`, () => { + expect(getCardValue("3♥")).toEqual(3); +}); + +test(`Should return 4 when given a number card (4♦)`, () => { + expect(getCardValue("4♦")).toEqual(4); +}); -// 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 +test(`Should return 5 when given a number card (5♣)`, () => { + expect(getCardValue("5♣")).toEqual(5); +}); + +test(`Should return 6 when given a number card (6♠)`, () => { + expect(getCardValue("6♠")).toEqual(6); +}); + +test(`Should return 7 when given a number card (7♥)`, () => { + expect(getCardValue("7♥")).toEqual(7); +}); + +test(`Should return 8 when given a number card (8♦)`, () => { + expect(getCardValue("8♦")).toEqual(8); +}); + +test(`Should return 9 when given a number card (9♣)`, () => { + expect(getCardValue("9♣")).toEqual(9); +}); + +test(`Should return 10 when given a number card (10♠)`, () => { + expect(getCardValue("10♠")).toEqual(10); +}); + +// Case 3: Face Cards (J, Q, K) - Should return 10 +test(`Should return 10 when given a Jack card (J♠)`, () => { + expect(getCardValue("J♠")).toEqual(10); +}); + +test(`Should return 10 when given a Queen card (Q♥)`, () => { + expect(getCardValue("Q♥")).toEqual(10); +}); + +test(`Should return 10 when given a King card (K♦)`, () => { + expect(getCardValue("K♦")).toEqual(10); +}); + +test(`Should return 10 when given a Jack card (J♣)`, () => { + expect(getCardValue("J♣")).toEqual(10); +}); + +test(`Should return 10 when given a Queen card (Q♠)`, () => { + expect(getCardValue("Q♠")).toEqual(10); +}); + +test(`Should return 10 when given a King card (K♥)`, () => { + expect(getCardValue("K♥")).toEqual(10); +}); + +// Case 4: Invalid Cards - Should throw error +test(`Should throw an error when given an invalid rank`, () => { + expect(() => getCardValue("X♠")).toThrow(); +}); + +test(`Should throw an error when given an invalid suit`, () => { + expect(() => getCardValue("A★")).toThrow(); +}); + +test(`Should throw an error when given an empty string`, () => { + expect(() => getCardValue("")).toThrow(); +}); + +test(`Should throw an error when given only a suit (no rank)`, () => { + expect(() => getCardValue("♠")).toThrow(); +}); + +test(`Should throw an error when given an invalid format`, () => { + expect(() => getCardValue("A")).toThrow(); +});