From 02bb64f13e71333d1f41b3e7cb2f668a22935923 Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Fri, 6 Mar 2026 21:20:11 +0000 Subject: [PATCH 1/2] Implement and rewrite test done --- .../implement/1-get-angle-type.js | 41 ++++++++++++- .../implement/2-is-proper-fraction.js | 15 +++++ .../implement/3-get-card-value.js | 61 ++++++++++++++++++- 3 files changed, 113 insertions(+), 4 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 9e05a871e2..2b34c0e35f 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 @@ -15,7 +15,27 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle > 0 && angle < 90) { + return "Acute angle"; + } + + if (angle === 90) { + return "Right 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"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +55,22 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +// Acute +assertEquals(getAngleType(45), "Acute angle"); + +// Right +assertEquals(getAngleType(90), "Right angle"); + +// Obtuse +assertEquals(getAngleType(120), "Obtuse angle"); + +// Straight +assertEquals(getAngleType(180), "Straight angle"); + +// Reflex +assertEquals(getAngleType(270), "Reflex angle"); + +// Invalid +assertEquals(getAngleType(0), "Invalid angle"); +assertEquals(getAngleType(360), "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 970cb9b641..ae20a224de 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 (denominator === 0) { + return false; + } + + 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,13 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(-1, 2), true); +assertEquals(isProperFraction(1, -2), true); + +// Improper fractions +assertEquals(isProperFraction(2, 2), false); +assertEquals(isProperFraction(3, 2), false); +assertEquals(isProperFraction(-3, 2), false); + +// Invalid denominator +assertEquals(isProperFraction(1, 0), 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 c7559e787e..fe6e5f8e4f 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,38 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + const validSuits = ["♠", "♥", "♦", "♣"]; + + if (!validSuits.includes(suit)) { + throw new Error("Invalid card"); + } + + if (rank === "A") { + return 11; + } + + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + + if ( + rank === "2" || + rank === "3" || + rank === "4" || + rank === "5" || + rank === "6" || + rank === "7" || + rank === "8" || + rank === "9" || + rank === "10" + ) { + return Number(rank); + } + + throw new Error("Invalid card"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -38,8 +69,19 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: + +// Ace +assertEquals(getCardValue("A♠"), 11); + +// Face cards +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♥"), 10); + +// Number cards +assertEquals(getCardValue("2♥"), 2); assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("10♦"), 10); // Handling invalid cards try { @@ -49,4 +91,17 @@ try { console.error("Error was not thrown for invalid card"); } catch (e) {} -// What other invalid card cases can you think of? +try { + getCardValue("1♠"); + console.error("Error was not thrown for invalid rank"); +} catch (e) {} + +try { + getCardValue("A"); + console.error("Error was not thrown for missing suit"); +} catch (e) {} + +try { + getCardValue("A★"); + console.error("Error was not thrown for invalid suit"); +} catch (e) {} From e0c6e0e5dbef8a5c63983e2bb97638aa274d344a Mon Sep 17 00:00:00 2001 From: Daniel Aderibigbe Date: Fri, 6 Mar 2026 22:08:33 +0000 Subject: [PATCH 2/2] practice-tdd completed --- Sprint-3/2-practice-tdd/count.js | 10 ++++++++- Sprint-3/2-practice-tdd/count.test.js | 15 +++++++++++++ Sprint-3/2-practice-tdd/get-ordinal-number.js | 21 ++++++++++++++++++- Sprint-3/2-practice-tdd/repeat-str.js | 8 +++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 21 +++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..6d3c47f54f 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let letter of stringOfCharacters) { + if (letter === findCharacter) { + count++; + } + } + + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..60a2d1e387 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,5 +1,6 @@ // implement a function countChar that counts the number of times a character occurs in a string const countChar = require("./count"); + // Given a string `str` and a single character `char` to search for, // When the countChar function is called with these inputs, // Then it should: @@ -22,3 +23,17 @@ test("should count multiple occurrences of a character", () => { // 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 count multiple occurrences of a character", () => { + const str = "aaaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); + +test("should return 0 when the character does not appear in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..5b4b8fb45c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,24 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; + const lastDigit = num % 10; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return number + "th"; + } + + if (lastDigit === 1) { + return num + "st"; + } + + if (lastDigit === 2) { + return num + "nd"; + } + + if (lastDigit === 3) { + return num + "rd"; + } + + return num + "th"; } module.exports = getOrdinalNumber; diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b003..193f1f8bd7 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,9 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + if (count < 0) { + throw new Error("Count cannot be negative"); + } + + return str.repeat(count); } module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..8837edc706 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -21,12 +21,33 @@ test("should repeat the string count times", () => { // 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; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); + // 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. +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // 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. + +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -1; + + expect(() => repeatStr(str, count)).toThrow(); +});