From 4b793d9818be529d4a99576c9eb1ea8c6e5f051a Mon Sep 17 00:00:00 2001 From: Rhoda Ajiroba Date: Mon, 9 Nov 2026 01:33:05 +0000 Subject: [PATCH 1/2] Implement functions and rewrite tests with Jest --- .../1-get-angle-type.test.js | 33 ++++++++++++++-- .../2-is-proper-fraction.test.js | 31 ++++++++++++--- .../3-get-card-value.test.js | 33 +++++++++++++--- .../implement/1-get-angle-type.js | 22 ++++++++++- .../implement/2-is-proper-fraction.js | 2 +- .../implement/3-get-card-value.js | 18 ++++++++- .../1-get-angle-type.test.js | 38 +++++++++++++------ .../2-is-proper-fraction.test.js | 28 ++++++++++++-- .../3-get-card-value.test.js | 34 +++++++++++------ 9 files changed, 196 insertions(+), 43 deletions(-) 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 cf50f6cade..361c05f03c 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 @@ -3,10 +3,35 @@ import test from "node:test"; import { getAngleType } from "../implement/1-get-angle-type.js"; -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles +test("Classifies acute angles", () => { + assert.equal(getAngleType(45), "Acute angle"); + assert.equal(getAngleType(1), "Acute angle"); + assert.equal(getAngleType(89), "Acute angle"); +}); test("Classifies right angles", () => { - const right = getAngleType(90); - assert.equal(right, "Right angle"); + assert.equal(getAngleType(90), "Right angle"); +}); + +test("Classifies obtuse angles", () => { + assert.equal(getAngleType(120), "Obtuse angle"); + assert.equal(getAngleType(91), "Obtuse angle"); + assert.equal(getAngleType(179), "Obtuse angle"); +}); + +test("Classifies straight angles", () => { + assert.equal(getAngleType(180), "Straight angle"); +}); + +test("Classifies reflex angles", () => { + assert.equal(getAngleType(270), "Reflex angle"); + assert.equal(getAngleType(181), "Reflex angle"); + assert.equal(getAngleType(359), "Reflex angle"); +}); + +test("Identifies invalid angles", () => { + assert.equal(getAngleType(0), "Invalid angle"); + assert.equal(getAngleType(360), "Invalid angle"); + assert.equal(getAngleType(-1), "Invalid angle"); + assert.equal(getAngleType(361), "Invalid angle"); }); 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 a290846058..16cc582dbd 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 @@ -3,10 +3,31 @@ import test from "node:test"; 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", () => { - // Example: 1/2 is a proper fraction +test("Identifies basic proper fractions", () => { assert.equal(isProperFraction(1, 2), true); + assert.equal(isProperFraction(3, 4), true); +}); + +test("Identifies improper fractions", () => { + assert.equal(isProperFraction(5, 3), false); + assert.equal(isProperFraction(4, 4), false); +}); + +test("Handles zero numerator", () => { + assert.equal(isProperFraction(0, 5), true); +}); + +test("Handles negative numerators", () => { + assert.equal(isProperFraction(-1, 2), true); + assert.equal(isProperFraction(-5, 2), false); +}); + +test("Handles negative denominators", () => { + assert.equal(isProperFraction(1, -2), true); + assert.equal(isProperFraction(3, -2), false); +}); + +test("Handles zero denominator", () => { + assert.equal(isProperFraction(1, 0), false); + assert.equal(isProperFraction(0, 0), false); }); 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 f5092d9b10..7678c58be2 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 @@ -3,14 +3,35 @@ import test from "node:test"; import { getCardValue } from "../implement/3-get-card-value.js"; -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. +test("Returns the correct value for number cards", () => { + assert.equal(getCardValue("2♠"), 2); + assert.equal(getCardValue("5♥"), 5); + assert.equal(getCardValue("9♦"), 9); + assert.equal(getCardValue("10♣"), 10); +}); + +test("Returns 11 for aces", () => { + assert.equal(getCardValue("A♠"), 11); +}); -test("Valid single-digit card", () => { - assert.equal(getCardValue("9♠"), 9); +test("Returns 10 for face cards", () => { + assert.equal(getCardValue("J♠"), 10); + assert.equal(getCardValue("Q♥"), 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"); +test("Accepts all four suits", () => { + assert.equal(getCardValue("5♠"), 5); + assert.equal(getCardValue("5♥"), 5); + assert.equal(getCardValue("5♦"), 5); + assert.equal(getCardValue("5♣"), 5); }); -// TODO: What other invalid card cases can you think of? +test("Rejects invalid cards", () => { + assert.throws(() => getCardValue("invalid")); + assert.throws(() => getCardValue("1♠")); + assert.throws(() => getCardValue("11♠")); + assert.throws(() => getCardValue("5X")); + assert.throws(() => getCardValue("♠5")); + assert.throws(() => getCardValue("5")); +}); 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 8a6fba0504..a2e2cb7d2b 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,25 @@ // execute the code to ensure all tests pass. export function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) { + return "Invalid angle"; + } + + if (angle < 90) { + return "Acute angle"; + } + + if (angle === 90) { + return "Right angle"; + } + + if (angle < 180) { + return "Obtuse angle"; + } + + if (angle === 180) { + return "Straight angle"; + } + + return "Reflex angle"; } 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 f983800f00..55ac4ceaeb 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 denominator !== 0 && Math.abs(numerator) < Math.abs(denominator); } 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 9614ab1ee9..f1b071aeca 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -24,5 +24,21 @@ // execute the code to ensure all tests pass. export function getCardValue(card) { - // TODO: Implement this function + const validCard = /^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/; + + if (!validCard.test(card)) { + throw new Error(`Expected a number followed by a suit, but got "${card}"`); + } + + const rank = card.slice(0, -1); + + if (rank === "A") { + return 11; + } + + if (["J", "Q", "K"].includes(rank)) { + return 10; + } + + return Number(rank); } 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 e0ba786ab3..533e4ebefe 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 @@ -1,18 +1,34 @@ import { getAngleType } from "../implement/1-get-angle-type.js"; -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. - -// Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { - // Test various acute angles, including boundary cases +test("Classifies acute angles", () => { expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); -// Case 2: Right angle -// Case 3: Obtuse angles -// Case 4: Straight angle -// Case 5: Reflex angles -// Case 6: Invalid angles +test("Classifies right angles", () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + +test("Classifies obtuse angles", () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + +test("Classifies straight angles", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + +test("Classifies reflex angles", () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + +test("Identifies invalid angles", () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-1)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid angle"); +}); 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 5d49e2d8bb..1645818c48 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 @@ -1,8 +1,30 @@ 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. +test("Identifies positive proper fractions", () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(3, 4)).toEqual(true); +}); + +test("Identifies improper fractions", () => { + expect(isProperFraction(5, 3)).toEqual(false); + expect(isProperFraction(4, 4)).toEqual(false); +}); + +test("Handles zero numerator", () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); + +test("Handles negative numerators", () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(-5, 2)).toEqual(false); +}); + +test("Handles negative denominators", () => { + expect(isProperFraction(1, -2)).toEqual(true); + expect(isProperFraction(3, -2)).toEqual(false); +}); -// Special case: numerator is zero -test(`should return false when denominator is zero`, () => { +test("Handles zero denominator", () => { expect(isProperFraction(1, 0)).toEqual(false); + expect(isProperFraction(0, 0)).toEqual(false); }); 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 a6de5a82a4..ca44e43d73 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 @@ -1,18 +1,30 @@ import { getCardValue } from "../implement/3-get-card-value.js"; -// TODO: Write tests in Jest syntax to cover all possible outcomes. - -// Case 1: Ace (A) -test(`Should return 11 when given an ace card`, () => { +test("Returns 11 for aces", () => { expect(getCardValue("A♠")).toEqual(11); + expect(getCardValue("A♥")).toEqual(11); + expect(getCardValue("A♦")).toEqual(11); + 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("Returns the correct value for number cards", () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("9♦")).toEqual(9); + expect(getCardValue("10♣")).toEqual(10); +}); -// 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("Returns 10 for face cards", () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); +}); +test("Throws an error for invalid cards", () => { + expect(() => getCardValue("invalid")).toThrow(); + expect(() => getCardValue("1♠")).toThrow(); + expect(() => getCardValue("11♠")).toThrow(); + expect(() => getCardValue("5X")).toThrow(); + expect(() => getCardValue("♠5")).toThrow(); + expect(() => getCardValue("5")).toThrow(); +}); From 0243e17e3042f9bfc20858ea642232ea06c177b6 Mon Sep 17 00:00:00 2001 From: Rhoda Ajiroba Date: Mon, 9 Nov 2026 02:23:25 +0000 Subject: [PATCH 2/2] Complete Practice TDD exercises --- practice-tdd/count.js | 2 +- practice-tdd/count.test.js | 19 +++++++++++----- practice-tdd/get-ordinal-number.js | 22 +++++++++++++++++- practice-tdd/get-ordinal-number.test.js | 25 +++++++++++++++++++++ practice-tdd/repeat-str.js | 16 +++++++++---- practice-tdd/repeat-str.test.js | 30 +++++++++++++++---------- 6 files changed, 91 insertions(+), 23 deletions(-) diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c57123..6167d64448 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,3 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + return stringOfCharacters.split(findCharacter).length - 1; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae591..2cb3d240d1 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,8 +17,17 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences -// Given the input string `str`, -// And a character `char` that does not exist within `str`. -// When the function is called with these inputs, -// Then it should return 0, indicating that no occurrences of `char` were found. +test("should return 0 when the character does not exist", () => { + const str = "hello"; + const char = "z"; + + const count = countChar(str, char); + + expect(count).toEqual(0); +}); +test("should count a single occurrence", () => { + expect(countChar("hello", "h")).toEqual(1); +}); +test("should return 0 for an empty string", () => { + expect(countChar("", "a")).toEqual(0); +}); diff --git a/practice-tdd/get-ordinal-number.js b/practice-tdd/get-ordinal-number.js index 5c6291f5ed..81dbae083b 100644 --- a/practice-tdd/get-ordinal-number.js +++ b/practice-tdd/get-ordinal-number.js @@ -1,3 +1,23 @@ export function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return `${num}th`; + } + + const lastDigit = num % 10; + + if (lastDigit === 1) { + return `${num}st`; + } + + if (lastDigit === 2) { + return `${num}nd`; + } + + if (lastDigit === 3) { + return `${num}rd`; + } + + return `${num}th`; } diff --git a/practice-tdd/get-ordinal-number.test.js b/practice-tdd/get-ordinal-number.test.js index e23174a187..741bd12905 100644 --- a/practice-tdd/get-ordinal-number.test.js +++ b/practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,28 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +test("should append 'nd' for numbers ending in 2, except those ending in 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(112)).toEqual("112th"); +}); + +test("should append 'rd' for numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(113)).toEqual("113th"); +}); + +test("should append 'th' for other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(20)).toEqual("20th"); +}); + +test("should append 'th' for numbers ending in 11, 12 and 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); diff --git a/practice-tdd/repeat-str.js b/practice-tdd/repeat-str.js index af273f3b01..d69a92e2e8 100644 --- a/practice-tdd/repeat-str.js +++ b/practice-tdd/repeat-str.js @@ -1,5 +1,13 @@ -export function repeatStr() { - // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). - // The goal is to re-implement that function, not to use it. - return "hellohellohello"; +export function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + let result = ""; + + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } diff --git a/practice-tdd/repeat-str.test.js b/practice-tdd/repeat-str.test.js index 8a7036102b..1efdf7ab8a 100644 --- a/practice-tdd/repeat-str.test.js +++ b/practice-tdd/repeat-str.test.js @@ -16,17 +16,23 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohello"); }); -// Case: handle count of 1: -// Given a target string `str` and a `count` equal to 1, -// When the repeatStr function is called with these inputs, -// Then it should return the original `str` without repetition. +test("should return the original string when count is 1", () => { + const str = "hello"; + const count = 1; -// Case: Handle count of 0: -// Given a target string `str` and a `count` equal to 0, -// When the repeatStr function is called with these inputs, -// Then it should return an empty string. + const repeatedStr = repeatStr(str, count); -// Case: Handle negative count: -// Given a target string `str` and a negative integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should throw an error, as negative counts are not valid. + expect(repeatedStr).toEqual("hello"); +}); +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + + const repeatedStr = repeatStr(str, count); + + expect(repeatedStr).toEqual(""); +}); + +test("should throw an error when count is negative", () => { + expect(() => repeatStr("hello", -1)).toThrow(); +});