From f960b94b8cf7d4e06f65da73b3c933401fcab18c Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Sun, 1 Mar 2026 19:58:48 +0000 Subject: [PATCH 1/6] Sprint 3: Implement functions and rewrite tests in Jest --- .../implement/1-get-angle-type.js | 29 ++++++++- .../implement/2-is-proper-fraction.js | 16 ++++- .../implement/3-get-card-value.js | 65 ++++++++++++++++--- 3 files changed, 96 insertions(+), 14 deletions(-) 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..207beec6d 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 @@ -6,7 +6,8 @@ // - "Obtuse angle" for angles greater than 90° and less than 180° // - "Straight angle" for exactly 180° // - "Reflex angle" for angles greater than 180° and less than 360° -// - "Invalid angle" for angles outside the valid range. +// - "(angle >= 0 || angle <= 90){ + // Invalid angle" for angles outside the valid range. // Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) @@ -15,7 +16,17 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // 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. @@ -33,5 +44,19 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles +const acute = getAngleType(45) +assertEquals(acute, "Acute angle"); const right = getAngleType(90); assertEquals(right, "Right angle"); +const obtuse = getAngleType(110) +assertEquals(obtuse, "Obtuse angle"); +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +const reflex = getAngleType(250); +assertEquals(reflex, "Reflex angle"); +const invalid = getAngleType(380); +assertEquals(invalid, "Invalid angle"); +const invalid2 = getAngleType(0); +assertEquals(invalid2, "Invalid angle"); +const invalid3 = getAngleType(-10); +assertEquals(invalid3, "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..9a0edb4b9 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 + if(numerator <= 0 || denominator <= 0){ + return false; + }else if(numerator < denominator){ + return true; + }else return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -20,8 +25,7 @@ module.exports = isProperFraction; // Here's our helper again function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, + console.assert(actualOutput === targetOutput, `Expected ${actualOutput} to equal ${targetOutput}` ); } @@ -30,4 +34,10 @@ function assertEquals(actualOutput, targetOutput) { // 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"); +assertEquals(isProperFraction(3, 2), "false"); +assertEquals(isProperFraction(1, 0), "false"); +assertEquals(isProperFraction(8, 9), "true"); +assertEquals(isProperFraction(0, 1), "false"); +assertEquals(isProperFraction(-4, 1), "false"); +assertEquals(isProperFraction(2, -4), "false"); \ No newline at end of file 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..33e4c32c2 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,9 +22,25 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function -} + if (typeof card !== "string") { + throw new Error("Invalid card"); + } + let rank = card.slice(0, -1); // Get everything except the last character + let suit = card.slice(-1); // Get the last character + + const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + if (rank === "A"){ + return 11; + }else if(rank.match(/J|Q|K/)){ + return 10; + }else if(rank.match(/^(10|[2-9])$/)){ + return Number(rank); + }else throw new Error("Invalid card"); +} // 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; @@ -36,17 +52,48 @@ function assertEquals(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("J♣"), 10); +assertEquals(getCardValue("Q♥"), 10); +assertEquals(getCardValue("K♠"), 10); +assertEquals(getCardValue("3♦"), 3); -// Handling invalid cards try { - getCardValue("invalid"); + getCardValue("J"); - // This line will not be reached if an error is thrown as expected + // The below line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); -} catch (e) {} - +} catch (e) { + console.log('Test passed for "J": caught error ->', e.message); +} // What other invalid card cases can you think of? + +try { + getCardValue("9X"); // invalid suit + console.error('Test failed for "9X": error was not thrown'); +} catch (e) { + console.log('Test passed for "9X": caught error ->', e.message); +} + +try { + getCardValue("1♠"); // invalid rank + console.error('Test failed for "1♠": error was not thrown'); +} catch (e) { + console.log('Test passed for "1♠": caught error ->', e.message); +} + +try { + getCardValue("0♥"); // invalid rank + console.error('Test failed for "0♥": error was not thrown'); +} catch (e) { + console.log('Test passed for "0♥": caught error ->', e.message); +} + +try { + getCardValue("ABC"); // completely wrong format + console.error('Test failed for "ABC": error was not thrown'); +} catch (e) { + console.log('Test passed for "ABC": caught error ->', e.message); +} From 773ddc9365926797b8d5e10f730299a366286059 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 10 Mar 2026 09:59:35 +0000 Subject: [PATCH 2/6] Jest tests have been implemented --- .../1-get-angle-type.test.js | 24 +++++++++++++++++++ .../2-is-proper-fraction.test.js | 15 ++++++++++++ .../3-get-card-value.test.js | 16 ++++++++++++- clear | 5 ++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 clear 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..1fa313ca1 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,31 @@ 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)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(150)).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)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(200)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle <= 0 or angle >= 360)`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-40)).toEqual("Invalid angle"); + expect(getAngleType(400)).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..1fcf12d7c 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,18 @@ 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 false when denominator is less than numerator`, () => { + expect(isProperFraction(2, 1)).toEqual(false); +}); +test("should return false when numerator is zero", ()=>{ + expect(isProperFraction(0, 1)).toEqual(false); +}); +test("should return false when numerator is a negative number", ()=>{ + expect(isProperFraction(-2, 2)).toEqual(false); +}); +test("should return false when denominator is a negative number", ()=>{ + expect(isProperFraction(2, -2)).toEqual(false); +}); +test("should return false where both numerator and denominator are negative numbers", ()=> { + expect(isProperFraction(-3, -4)).toEqual(false); +}) \ No newline at end of file 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..d7e23a705 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 @@ -11,9 +11,23 @@ test(`Should return 11 when given an ace card`, () => { // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) +test("Should return the value of rank variable as a number when we are given 2-10", ()=>{ + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("9♠")).toEqual(9); + expect(getCardValue("10♠")).toEqual(10); +}); // Face Cards (J, Q, K) +test(`Should return 10 when given J or Q or K`, () => { + expect(getCardValue("K♠")).toEqual(10); + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♠")).toEqual(10); +}) // Invalid Cards - +test("Should return Invalid card", ()=>{ + expect(() => getCardValue(0)).toThrow("Invalid card"); + expect(() => getCardValue("1X")).toThrow("Invalid card"); + expect(() => getCardValue("0♥")).toThrow("Invalid card"); +}); // 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 diff --git a/clear b/clear new file mode 100644 index 000000000..a0dbb9c0d --- /dev/null +++ b/clear @@ -0,0 +1,5 @@ + acoursework/sprint-2 + acoursework/sprint-2-clean + coursework/sprint-1 +* coursework/sprint-3-implement-and-rewrite + main From fc8717b87763cb76323c1942c1ecb83b0625ac56 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 10 Mar 2026 10:08:52 +0000 Subject: [PATCH 3/6] Get-angle-type codes have been formated --- .../implement/1-get-angle-type.js | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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 207beec6d..5492c5a43 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 @@ -7,7 +7,7 @@ // - "Straight angle" for exactly 180° // - "Reflex angle" for angles greater than 180° and less than 360° // - "(angle >= 0 || angle <= 90){ - // Invalid angle" for angles outside the valid range. +// Invalid angle" for angles outside the valid range. // Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) @@ -16,17 +16,17 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - if(angle > 0 && angle < 90){ + if (angle > 0 && angle < 90) { return "Acute angle"; - }else if(angle === 90){ + } else if (angle === 90) { return "Right angle"; - }else if(angle > 90 && angle < 180){ - return "Obtuse angle" - }else if(angle === 180){ + } else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } else if (angle === 180) { return "Straight angle"; - }else if(angle > 180 && angle < 360){ + } else if (angle > 180 && angle < 360) { return "Reflex angle"; - }else return "Invalid angle"; + } else return "Invalid angle"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -44,11 +44,11 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles -const acute = getAngleType(45) +const acute = getAngleType(45); assertEquals(acute, "Acute angle"); const right = getAngleType(90); assertEquals(right, "Right angle"); -const obtuse = getAngleType(110) +const obtuse = getAngleType(110); assertEquals(obtuse, "Obtuse angle"); const straight = getAngleType(180); assertEquals(straight, "Straight angle"); @@ -59,4 +59,4 @@ assertEquals(invalid, "Invalid angle"); const invalid2 = getAngleType(0); assertEquals(invalid2, "Invalid angle"); const invalid3 = getAngleType(-10); -assertEquals(invalid3, "Invalid angle"); \ No newline at end of file +assertEquals(invalid3, "Invalid angle"); From 9d4539d400b1527dd805e79f9c91ead7fd73e5a6 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 10 Mar 2026 10:34:18 +0000 Subject: [PATCH 4/6] Implemented Jest test for tricky invalid cards --- .../rewrite-tests-with-jest/3-get-card-value.test.js | 6 ++++++ 1 file changed, 6 insertions(+) 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 d7e23a705..a10b60bbc 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 @@ -28,6 +28,12 @@ test("Should return Invalid card", ()=>{ expect(() => getCardValue("1X")).toThrow("Invalid card"); expect(() => getCardValue("0♥")).toThrow("Invalid card"); }); +// Invalid card format +test("Should throw error for tricky invalid card formats", () => { + expect(() => getCardValue("0x02♠")).toThrow("Invalid card"); + expect(() => getCardValue("QQ♠")).toThrow("Invalid card"); + expect(() => getCardValue("2.1♠")).toThrow("Invalid card"); +}); // 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 From 694f1cf10a3c7a6e26b516ccb137a6f57d21946e Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 10 Mar 2026 10:35:48 +0000 Subject: [PATCH 5/6] jest test implemented for tricky invalid cards --- .../1-implement-and-rewrite-tests/implement/3-get-card-value.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 33e4c32c2..65b1ff8cd 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 @@ -35,7 +35,7 @@ function getCardValue(card) { if (rank === "A"){ return 11; - }else if(rank.match(/J|Q|K/)){ + }else if(/^[JQK]$/.test(rank)){ return 10; }else if(rank.match(/^(10|[2-9])$/)){ return Number(rank); From 10c634764d7ebe9bdd09332d1d2c4347886154ea Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Tue, 10 Mar 2026 11:03:08 +0000 Subject: [PATCH 6/6] isProperFraction is updated --- .../implement/2-is-proper-fraction.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 9a0edb4b9..7ac6acd51 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,9 +12,9 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function - if(numerator <= 0 || denominator <= 0){ + if(denominator === 0){ return false; - }else if(numerator < denominator){ + }else if(Math.abs(numerator) < Math.abs(denominator)){ return true; }else return false; }