From c9ae22151e7a4a1957e2f5a85cdfa09b19ea68e3 Mon Sep 17 00:00:00 2001 From: FarancisGH Date: Sat, 7 Mar 2026 15:41:17 +0000 Subject: [PATCH 1/2] Adjusments made on Sprint 3 - Implement and rewrite tests --- .../implement/1-get-angle-type.js | 37 ++++++++++++++ .../implement/2-is-proper-fraction.js | 20 ++++++++ .../implement/3-get-card-value.js | 48 +++++++++++++++++++ 3 files changed, 105 insertions(+) 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..91d9eae91 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,11 +16,29 @@ function getAngleType(angle) { // TODO: Implement this function + // You can use if-else statements or a switch statement to determine the type of angle based on the input value. + 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; +// You can run this file with node to check your implementation, but you will need to write tests to cover all cases, including boundary and invalid cases. +// Example: Identify Right Angles +const right = getAngleType(90); +console.log(right); // Output: "Right angle" // This helper function is written to make our assertions easier to read. // If the actual output matches the target output, the test will pass @@ -31,7 +49,26 @@ function assertEquals(actualOutput, 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"); +// Example: Identify Acute Angles +const acute = getAngleType(45); +assertEquals(acute, "Acute angle"); +// Example: Identify Obtuse Angles +const obtuse = getAngleType(120); +assertEquals(obtuse, "Obtuse angle"); +// Example: Identify Straight Angles +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +// Example: Identify Reflex Angles +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); +// Example: Identify Invalid Angles +const invalid = getAngleType(-10); +assertEquals(invalid, "Invalid angle"); +const invalid2 = getAngleType(360); +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 970cb9b64..278dc1f99 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,6 +12,11 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + // A proper fraction is a fraction where the absolute value of the numerator is less than the absolute value of the denominator. Additionally, the denominator cannot be zero. Therefore, we can check if the absolute value of the numerator is less than the absolute value of the denominator and if the denominator is not zero to determine if it is a proper fraction. + if (denominator === 0) { + return false; // A fraction cannot have a denominator of zero + } + return Math.abs(numerator) < Math.abs(denominator); } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +36,18 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +// Example: 2/1 is not a proper fraction +assertEquals(isProperFraction(2, 1), false); +// Example: -1/2 is a proper fraction +assertEquals(isProperFraction(-1, 2), true); +// Example: 1/-2 is a proper fraction +assertEquals(isProperFraction(1, -2), true); +// Example: -1/-2 is a proper fraction +assertEquals(isProperFraction(-1, -2), true); +// Example: 0/5 is a proper fraction +assertEquals(isProperFraction(0, 5), true); +// Example: 5/0 is not a proper fraction +assertEquals(isProperFraction(5, 0), false); +// Example: 5/5 is not a proper fraction +assertEquals(isProperFraction(5, 5), 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..f20f1e419 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,6 +23,27 @@ 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 + + // Define valid ranks and suits + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + const validSuits = ["♠", "♥", "♦", "♣"]; + + // Check if the rank and suit are valid + if (!validRanks.includes(rank) || !validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + + // Determine the value based on the rank + if (rank === "A") { + return 11; + } else if (["J", "Q", "K"].includes(rank)) { + return 10; + } else { + return parseInt(rank); // Convert number cards to their numeric value + } + } // The line below allows us to load the getCardValue function into tests in other files. @@ -40,6 +61,12 @@ 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); + +// You should also test the boundary cases, such as "10♥", and invalid cases, such as "11♠" or "A" or "♠". // Handling invalid cards try { @@ -49,4 +76,25 @@ try { console.error("Error was not thrown for invalid card"); } catch (e) {} + // What other invalid card cases can you think of? +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 (e) {} + +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 (e) {} + +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 (e) {} From 47fad6502b1d9db3b9d992265ca16f2f47c65813 Mon Sep 17 00:00:00 2001 From: FarancisGH Date: Sat, 7 Mar 2026 16:29:43 +0000 Subject: [PATCH 2/2] Adjusments made on rewriting tests with Jest for the getAngleType function. Added an example test case for right angles and a comment to indicate where to write additional tests. Also added explanations for the implementation of the isProperFraction function and the multiply function in the previous sprint. --- .../1-get-angle-type.test.js | 7 ++++ .../2-is-proper-fraction.test.js | 36 +++++++++++++++++++ .../3-get-card-value.test.js | 22 ++++++++++++ 3 files changed, 65 insertions(+) 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..336ef1185 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 @@ -1,9 +1,15 @@ // This statement loads the getAngleType function you wrote in the implement directory. // 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. +// Example: Identify Right Angles +test(`should return "Right angle" when angle is 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 1: Acute angles test(`should return "Acute angle" when (0 < angle < 90)`, () => { @@ -18,3 +24,4 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { // Case 4: Straight angle // Case 5: Reflex angles // Case 6: Invalid angles + 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..8634cc806 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 @@ -3,6 +3,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. +// Example: 1/2 is a proper fraction +test(`should return true for 1/2`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); +// Example: 2/1 is not a proper fraction +test(`should return false for 2/1`, () => { + expect(isProperFraction(2, 1)).toEqual(false); +}); +// Example: -1/2 is a proper fraction +test(`should return true for -1/2`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); +}); +// Example: 1/-2 is a proper fraction +test(`should return true for 1/-2`, () => { + expect(isProperFraction(1, -2)).toEqual(true); +}); +// Example: -1/-2 is a proper fraction +test(`should return true for -1/-2`, () => { + expect(isProperFraction(-1, -2)).toEqual(true); +}); +// Example: 0/5 is a proper fraction +test(`should return true for 0/5`, () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); +// Example: 5/0 is not a proper fraction +test(`should return false for 5/0`, () => { + expect(isProperFraction(5, 0)).toEqual(false); +}); +// Example: 5/5 is not a proper fraction +test(`should return false for 5/5`, () => { + expect(isProperFraction(5, 5)).toEqual(false); +}); +// Special case: numerator is zero +test(`should return false when denominator is zero`, () => { + expect(isProperFraction(1, 0)).toEqual(false); +}); // Special case: numerator is zero test(`should return false when denominator is zero`, () => { 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..49bcee5d9 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 @@ -3,6 +3,27 @@ const getCardValue = require("../implement/3-get-card-value"); // TODO: Write tests in Jest syntax to cover all possible outcomes. +// Examples: +test(`should return 9 for "9♠"`, () => { + expect(getCardValue("9♠")).toEqual(9); +}); + +test(`should return 11 for "A♥"`, () => { + expect(getCardValue("A♥")).toEqual(11); +}); + +test(`should return 10 for "J♦"`, () => { + expect(getCardValue("J♦")).toEqual(10); +}); + +test(`should return 10 for "Q♣"`, () => { + expect(getCardValue("Q♣")).toEqual(10); +}); + +test(`should return 10 for "K♠"`, () => { + expect(getCardValue("K♠")).toEqual(10); +}); + // Case 1: Ace (A) test(`Should return 11 when given an ace card`, () => { @@ -14,6 +35,7 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards + // 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