From 2ec464dbc48ff65549e7adba5c23cee37b311fe4 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 5 Oct 2026 16:38:51 +0200 Subject: [PATCH 1/9] Refactor time conversion logic and enhance test coverage for edge cases --- format-clock-edge-cases/timeConverter.js | 10 +++- format-clock-edge-cases/timeConverter.test.js | 48 ++++++++++++++++++- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..95b539be7 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,17 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + const minutes = time.slice(2); + + if (hours === 24) { + return `12${minutes} am` + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${hours - 12}${minutes} pm`; } + return `${time} am`; } -export {formatAs12HourClock}; +export {formatAs12HourClock}; \ No newline at end of file diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..95a7aa1c5 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,10 +2,54 @@ import {formatAs12HourClock} from "./timeConverter.js"; import assert from "node:assert"; import test from "node:test"; -test("correctly convert time after 12:00", function(){ +test("correctly convert hours after 12:00", () => { assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); }); -test("can correctly convert morning time", function() { +test("correctly convert minutes after 12:00", () => { + assert.equal(formatAs12HourClock("23:53"), "11:53 pm"); +}); + +test("can correctly convert morning time", () => { assert.equal(formatAs12HourClock("08:00"), "08:00 am"); }); + +test("can correctly convert morning time minutes", () => { + assert.equal(formatAs12HourClock("08:30"), "08:30 am"); +}); + +test("can correctly convert morning time minutes", () => { + assert.equal(formatAs12HourClock("08:34"), "08:34 am"); +}); + +test("can successfully convert midnight time", () => + assert.equal(formatAs12HourClock("24:00"), "12:00 am")); + + + +/* +Edge cases: +Correctly convert hours after 12:00 +Input | Expected +23:00 11:00 pm + +Correctly convert afternoon hours and minutes +Input | Expected +23:00 11:53 pm + +Correctly convert morning time and minutes +Input | Expected +08:00 08:00 am + +Correctly convert morning time and minutes +Input | Expected +08:30 08:30 am + +Correctly convert morning time and minutes +Input | Expected +08:34 08:34 am + +Correctly convert midnight time +Input | Expected +24:00 12:00 am +*/ \ No newline at end of file From 0795731b4f4347e7ad275b61e3e24995418d827c Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Wed, 7 Oct 2026 17:44:58 +0200 Subject: [PATCH 2/9] Refactor time conversion logic and add tests for edge cases including midnight and noon --- format-clock-edge-cases/timeConverter.js | 20 ++++++++---- format-clock-edge-cases/timeConverter.test.js | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 95b539be7..82c82a643 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,17 +1,23 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - const minutes = time.slice(2); - if (hours === 24) { - return `12${minutes} am` + const [hoursStr, minutesStr] = time.split(':'); + const hours = Number(hoursStr); + const minutes = minutesStr; + + if (hours === 0 || hours === 24) { + return `12:${minutes} am` } - if (hours > 12) { - return `${hours - 12}${minutes} pm`; + if (hours === 12) { + return `12:${minutes} pm` } - return `${time} am`; + if (hours > 12) { + return `${hours - 12}:${minutes} pm`; + } + const formattedHours = String(hours).padStart(2, '0'); + return `${formattedHours}:${minutes} am`; } export {formatAs12HourClock}; \ No newline at end of file diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 95a7aa1c5..22ee9b448 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -25,7 +25,22 @@ test("can correctly convert morning time minutes", () => { test("can successfully convert midnight time", () => assert.equal(formatAs12HourClock("24:00"), "12:00 am")); +test("correctly convert midnight 00:00", () => { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("correctly convert noon 12:00", () => { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); +test("correctly convert boundary time around noon", () => { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); +}); + +test("correctly convert early afternoon hours", () => { + assert.equal(formatAs12HourClock("13:00"), "1:00 pm"); +}); /* Edge cases: @@ -52,4 +67,21 @@ Input | Expected Correctly convert midnight time Input | Expected 24:00 12:00 am + +Correctly convert midnight +Input | Expected +00:00 12:00 am + +Correctly convert noon +Input | Expected +12:00 12:00 pm + +Correctly convert boundary time around noon +Input | Expected +11:59 11:59 am +12:01 12:01 pm + +Correctly convert early afternoon hours +Input | Expected +13:00 1:00 pm */ \ No newline at end of file From 7753fc4912c98d0ca458f6795b624223c2416b7a Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Wed, 7 Oct 2026 17:45:10 +0200 Subject: [PATCH 3/9] Enhance test coverage for single-digit morning hours in time conversion --- format-clock-edge-cases/timeConverter.js | 4 ++-- format-clock-edge-cases/timeConverter.test.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 82c82a643..7dc67c505 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,6 +1,5 @@ function formatAs12HourClock(time) { - - + const [hoursStr, minutesStr] = time.split(':'); const hours = Number(hoursStr); const minutes = minutesStr; @@ -16,6 +15,7 @@ function formatAs12HourClock(time) { if (hours > 12) { return `${hours - 12}:${minutes} pm`; } + const formattedHours = String(hours).padStart(2, '0'); return `${formattedHours}:${minutes} am`; } diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 22ee9b448..9420e6215 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -42,6 +42,10 @@ test("correctly convert early afternoon hours", () => { assert.equal(formatAs12HourClock("13:00"), "1:00 pm"); }); +test("correctly convert single-digit morning hours if supported", () => { + assert(formatAs12HourClock("08:30"), "8:30am"); +}); + /* Edge cases: Correctly convert hours after 12:00 @@ -84,4 +88,8 @@ Input | Expected Correctly convert early afternoon hours Input | Expected 13:00 1:00 pm + +Correctly convert single-digit morning hours if supported +Input | Expected +08:30 8:30 am */ \ No newline at end of file From f357b30a32dad2ede4156f4297517fe1b9f318e7 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Sat, 10 Oct 2026 18:52:58 +0200 Subject: [PATCH 4/9] Fix formatting issues in time conversion and update test assertions for single-digit morning hours --- format-clock-edge-cases/timeConverter.js | 7 ++++--- format-clock-edge-cases/timeConverter.test.js | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 7dc67c505..dd53f4e71 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -5,15 +5,16 @@ function formatAs12HourClock(time) { const minutes = minutesStr; if (hours === 0 || hours === 24) { - return `12:${minutes} am` + return `12:${minutes} am`; } if (hours === 12) { - return `12:${minutes} pm` + return `12:${minutes} pm`; } if (hours > 12) { - return `${hours - 12}:${minutes} pm`; + const formattedHours = String(hours - 12).padStart(2, '0'); + return `${formattedHours}:${minutes} pm`; } const formattedHours = String(hours).padStart(2, '0'); diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 9420e6215..a9608c505 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -39,11 +39,11 @@ test("correctly convert boundary time around noon", () => { }); test("correctly convert early afternoon hours", () => { - assert.equal(formatAs12HourClock("13:00"), "1:00 pm"); + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); }); test("correctly convert single-digit morning hours if supported", () => { - assert(formatAs12HourClock("08:30"), "8:30am"); + assert(formatAs12HourClock("08:30"), "08:30am"); }); /* From b52295f2e463d5640c6521e896b216f019075ef5 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 12 Oct 2026 12:58:54 +0200 Subject: [PATCH 5/9] Implement angle classification logic and enhance test coverage for all angle types --- .../1-get-angle-type.test.js | 38 +++++++++++++++++++ .../implement/1-get-angle-type.js | 21 +++++++++- .../1-get-angle-type.test.js | 31 +++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js index cf50f6cad..552a45175 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js @@ -10,3 +10,41 @@ test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); + +test("classifies acute angles", () => { + assert.equal(getAngleType(1), "Acute angle"); + assert.equal(getAngleType(45), "Acute angle"); + assert.equal(getAngleType(89), "Acute angle"); + assert.equal(getAngleType(36), "Acute angle"); + assert.equal(getAngleType(20), "Acute angle"); + +}); + +test("classifies obtuse angles", () => { + assert.equal(getAngleType(91), "Obtuse angle"); + assert.equal(getAngleType(105), "Obtuse angle"); + assert.equal(getAngleType(179), "Obtuse angle"); + assert.equal(getAngleType(125), "Obtuse angle"); + assert.equal(getAngleType(110), "Obtuse angle"); +}); + +test("classifies straight angles", () => { + const straight = getAngleType(180); + assert.equal(straight, "Straight angle"); +}); + +test("classifies reflex angles", () => { + assert.equal(getAngleType(181), "Reflex angle"); + assert.equal(getAngleType(220), "Reflex angle"); + assert.equal(getAngleType(289), "Reflex angle"); + assert.equal(getAngleType(300), "Reflex angle"); + assert.equal(getAngleType(349), "Reflex angle"); +}); + +test("classifies invalid angles", () => { + assert.equal(getAngleType(-5), "Invalid angle"); + assert.equal(getAngleType(0), "Invalid angle"); + assert.equal(getAngleType(360), "Invalid angle"); + assert.equal(getAngleType(400), "Invalid angle"); + assert.equal(getAngleType(420), "Invalid angle"); +}); \ No newline at end of file diff --git a/implement-and-rewrite-tests/implement/1-get-angle-type.js b/implement-and-rewrite-tests/implement/1-get-angle-type.js index 8a6fba050..5d4f57086 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -17,5 +17,24 @@ // execute the code to ensure all tests pass. export function getAngleType(angle) { - // TODO: Implement this function + if (angle === 90) { + return "Right angle"; + } + + if (angle > 0 && angle < 90) { + return "Acute angle"; + } + + if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } + + if (angle === 180) { + return "Straight angle"; + } + if (angle > 180 && angle < 360) { + return "Reflex angle"; + } + + return "Invalid angle"; } diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index e0ba786ab..816706085 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -12,7 +12,38 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle === 90)`, () => { + // Test the exact right-angle boundary + 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(120)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + // Test the exact straight-angle boundary + 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(220)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when (angles outside of valid range)`, () => { + // Test various invalid angles, including boundary cases + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-5)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); +}); From 46095d0dc3afc39024ac180b34cee439f25c5866 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 12 Oct 2026 14:25:39 +0200 Subject: [PATCH 6/9] Implement isProperFraction function and enhance test coverage for proper fractions --- .../2-is-proper-fraction.test.js | 70 ++++++++++++++++++- .../implement/2-is-proper-fraction.js | 2 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index a29084605..eee60bbcb 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -6,7 +6,75 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? -test("Basic proper fraction", () => { +test("Returns true for a basic proper fraction", () => { // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); }); + +test("Returns true for 2/8", () => { + assert.equal(isProperFraction(2, 8), true); +}); + +test("Returns true when the numerator is zero and denominator is positive (0/1)", () => { + assert.equal(isProperFraction(0, 1), true); +}); + +test("Returns true for 5/8", () => { + assert.equal(isProperFraction(5, 8), true); +}); + +test("Returns true for 8/9", () => { + assert.equal(isProperFraction(8, 9), true); +}); + +test("Returns true for another proper fraction with a smaller numerator", () => { + assert.equal(isProperFraction(2, 4), true); +}); + +test("Returns true when both numerator and denominator is negative", () => { + assert.equal(isProperFraction(-1, -2), true); +}); + +test("Returns true when the denominator is negative", () => { + assert.equal(isProperFraction(1, -2), true); +}); + +test("Returns true when the numerator is negative", () => { + assert.equal(isProperFraction(-1, 2), true); +}); + +test("Returns false when the numerator is greater than the denominator", () => { + assert.equal(isProperFraction(9, 8), false); +}); + +test("Returns false when numerator is negative and denominator is zero", () => { + assert.equal(isProperFraction(-1, 0), false); +}); + +test("Returns false for 4/2", () => { + assert.equal(isProperFraction(4, 2), false); +}); + +test("Returns false for 5/3", () => { + assert.equal(isProperFraction(5, 3), false); +}); + +test("Returns false for 6/4", () => { + assert.equal(isProperFraction(6, 4), false); +}); + +test("Returns false for 7/1", () => { + assert.equal(isProperFraction(7, 1), false); +}); + +test("Returns false for 15/14", () => { + assert.equal(isProperFraction(15, 14), false); +}); + +test("Returns false when numerator equals denominator", () => { + assert.equal(isProperFraction(4, 4), false); +}); + +test("Returns false when numerator is positive and denominator is zero", () => { + assert.equal(isProperFraction(1,0), false); +}) diff --git a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js index f983800f0..a5074973c 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -14,5 +14,5 @@ // execute the code to ensure all tests pass. export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return Math.abs(numerator) < Math.abs(denominator) } From 6b7a16f7326dc09124dae4bbcadc2d1b6d7448d2 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 12 Oct 2026 14:48:04 +0200 Subject: [PATCH 7/9] Refactor test descriptions for clarity in isProperFraction tests --- .../2-is-proper-fraction.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index eee60bbcb..5c4da80a7 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -15,7 +15,7 @@ test("Returns true for 2/8", () => { assert.equal(isProperFraction(2, 8), true); }); -test("Returns true when the numerator is zero and denominator is positive (0/1)", () => { +test("Returns true when the numerator is zero and denominator is positive", () => { assert.equal(isProperFraction(0, 1), true); }); @@ -35,11 +35,11 @@ test("Returns true when both numerator and denominator is negative", () => { assert.equal(isProperFraction(-1, -2), true); }); -test("Returns true when the denominator is negative", () => { +test("Returns true when the numerator is positive denominator is negative", () => { assert.equal(isProperFraction(1, -2), true); }); -test("Returns true when the numerator is negative", () => { +test("Returns true when the numerator is negative and the denominator is positive", () => { assert.equal(isProperFraction(-1, 2), true); }); From ac8efd14727a5c7ed0e4ac3696ff787051b17e1a Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 12 Oct 2026 14:51:25 +0200 Subject: [PATCH 8/9] Enhance test coverage for isProperFraction function with additional cases --- .../2-is-proper-fraction.test.js | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 5d49e2d8b..5008295fc 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -2,7 +2,80 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. -// Special case: numerator is zero +// // Special case: numerator is zero +// test(`should return false when denominator is zero`, () => { +// expect(isProperFraction(1, 0)).toEqual(false); +// }); + +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +test(`should return false when numerator equals the denominator`, () => { + expect(isProperFraction(4, 4)).toEqual(false); +}); + +test(`should return false if 15/14`, () => { + expect(isProperFraction(15, 14)).toEqual(false); +}); + +test(`should return false if 7/1`, () => { + expect(isProperFraction(7, 1)).toEqual(false); +}); + +test(`should return false if 6/4`, () => { + expect(isProperFraction(6, 4)).toEqual(false); +}); + +test(`should return false if 5/3`, () => { + expect(isProperFraction(5, 3)).toEqual(false); +}); + +test(`should return false if 4/2`, () => { + expect(isProperFraction(4, 2)).toEqual(false); +}); + +test(`should return false if numerator is negative and denominator is zero`, () => { + expect(isProperFraction(-1, 0)).toEqual(false); +}); + +test(`should return false when the numerator is greater than the denominator`, () => { + expect(isProperFraction(9, 8)).toEqual(false); +}); + +test(`should return true if numerator is negative and denominator is positive`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); +}); + +test(`should return true if numerator is positive and denominator is negative`, () => { + expect(isProperFraction(1, -2)).toEqual(true); +}); + +test(`should return true if both numerator and denominator is negative`, () => { + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +test(`should return true for another proper fraction with a smaller numerator`, () => { + expect(isProperFraction(2, 4)).toEqual(true); +}); + +test(`should return true if 8/9`, () => { + expect(isProperFraction(8, 9)).toEqual(true); +}); + +test(`should return true if 5/8`, () => { + expect(isProperFraction(5, 8)).toEqual(true); +}); + +test(`should return true if numerator is zero and the denominator is positive`, () => { + expect(isProperFraction(0, 1)).toEqual(true); +}); + +test(`should return true 2/8`, () => { + expect(isProperFraction(2, 8)).toEqual(true); +}); + +test(`should return true for a basic proper fraction`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); \ No newline at end of file From d1e881988396d44b10b719afab5fd5ac2b25fb08 Mon Sep 17 00:00:00 2001 From: chefmatthaus-hub Date: Mon, 12 Oct 2026 16:35:42 +0200 Subject: [PATCH 9/9] Implement getCardValue function and enhance test coverage for valid and invalid card cases --- .../3-get-card-value.test.js | 47 ++++++++++++++++- .../implement/3-get-card-value.js | 26 +++++++++- .../3-get-card-value.test.js | 51 ++++++++++++++++++- 3 files changed, 120 insertions(+), 4 deletions(-) diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js index f5092d9b1..78fe108fb 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js @@ -9,8 +9,53 @@ test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); +test("Ace has a value of 11", () => { + assert.equal(getCardValue("A♠"), 11); +}); + +test("Two has a value of 2", () => { + assert.equal(getCardValue("2♥"), 2); +}); + +test("Ten has a value of 10", () => { + assert.equal(getCardValue("10♥"), 10); +}); + +test("Jack has a value of 10", () => { + assert.equal(getCardValue("J♣"), 10); +}); + +test("Queen has a value of 10", () => { + assert.equal(getCardValue("Q♦"), 10); +}); + +test("King has a value of 10", () => { + assert.equal(getCardValue("K♦"), 10); +}); + test("Arbitrary non-card string", () => { assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); }); -// TODO: What other invalid card cases can you think of? +test("Invalid rank throws an error", () => { + assert.throws(() => getCardValue("Z♠"), /Expected a number followed by a suit, but got "Z♠"/, "Expected clear error"); +}); + +test("Invalid suit throws an error", () => { + assert.throws(() => getCardValue("9X"), /Expected a number followed by a suit, but got "9X"/, "Expected clear error"); +}); + +test("Card without a suit throws an error", () => { + assert.throws(() => getCardValue("9"), /Expected a number followed by a suit, but got "9"/, "Expected clear error"); +}); + +test("Card without a rank throws an error", () => { + assert.throws(() => getCardValue("♠"), /Expected a number followed by a suit, but got "♠"/, "Expected clear error"); +}); + +test("Card without a number greater than 10 throws an error", () => { + assert.throws(() => getCardValue("15♠"), /Expected a number followed by a suit, but got "15♠"/, "Expected clear error"); +}); + + +// TODO: What other invalid card cases can you think of? \ No newline at end of file diff --git a/implement-and-rewrite-tests/implement/3-get-card-value.js b/implement-and-rewrite-tests/implement/3-get-card-value.js index 9614ab1ee..3bbf8aef2 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -23,6 +23,28 @@ // After you have implemented the function, write tests to cover all the cases, and // execute the code to ensure all tests pass. + export function getCardValue(card) { - // TODO: Implement this function -} + const rank = card.slice(0, -1); + const suit = card.slice(-1); + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + + if (!validSuits.includes(suit)) { + throw new Error (`Expected a number followed by a suit, but got "${card}"`); + } + + if (!validRanks.includes(rank)) { + throw new Error(`Expected a number followed by a suit, but got "${card}"`); + } + + if (rank === "A") { + return 11; + } + + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + + return Number(rank); +} \ No newline at end of file diff --git a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index a6de5a82a..3c62c0b28 100644 --- a/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -7,6 +7,56 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +// Case 2: Ranks (Number Cards) +test("Two has a value of 2", () => { + expect(getCardValue("2♥")).toEqual (2); +}); + +test("Five has a value of 2", () => { + expect(getCardValue("5♦")).toEqual (5); +}); + +test("Ten has a value of 10", () => { + expect(getCardValue("10♥")).toEqual(10); +}); + + +//Case 3: Face cards (J, Q, K) +test("Jack has a value of 10", () => { + expect(getCardValue("J♣")).toEqual(10); +}); + +test("Queen has a value of 10", () => { + expect(getCardValue("Q♦")).toEqual(10); +}); + +test("King has a value of 10", () => { + expect(getCardValue("K♦")).toEqual(10); +}); + + +//Case 4: Invalid cards +test("Card without a suit throws an error", () => { + expect(() => getCardValue("9")).toThrow(); +}); + +test("Card without a number throws an error", () => { + expect(() => getCardValue("♣")).toThrow(); +}); + +test("Card with invalid suit throws an error", () => { + expect(() => getCardValue("9X")).toThrow(); +}); + +test("Card with invalid rank throws an error", () => { + expect(() => getCardValue("X♣")).toThrow(); +}); + +test("Card with rank higher than 10 throws an error", () => { + expect(() => getCardValue("15♣")).toThrow(); +}); + + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K) @@ -15,4 +65,3 @@ test(`Should return 11 when given an ace 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 -