From b5de08b92f56bc5424c222b51101f87db10599ef Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Thu, 9 Oct 2025 19:31:45 +0100 Subject: [PATCH 01/32] build up getAngleType case by case --- .../implement/1-get-angle-type.js | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 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 ca1dfe7f2..207ff21da 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 @@ -11,8 +11,19 @@ function getAngleType(angle) { if (angle === 90) { return "Right angle"; } - // Run the tests, work out what Case 2 is testing, and implement the required code here. - // Then keep going for the other cases, one at a time. + if (angle < 90 && angle > 0) { + 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"; } // The line below allows us to load the getAngleType function into tests in other files. @@ -50,14 +61,33 @@ assertEquals(acute, "Acute angle"); // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" const obtuse = getAngleType(120); -// ====> write your test here, and then add a line to pass the test in the function above +assertEquals(obtuse, "Obtuse angle"); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" // ====> write your test here, and then add a line to pass the test in the function above - +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> write your test here, and then add a line to pass the test in the function above +const reflex = getAngleType(270); +assertEquals(reflex, "Reflex angle"); + +let angle = 120; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = 45; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = 90; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = 180; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = 270; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = -10; +console.log(angle + " degrees is a " + getAngleType(angle)); +angle = 400; +console.log(angle + " degrees is a " + getAngleType(angle)); +// Case 6: Handle Invalid Angles: \ No newline at end of file From b5fa5623635ba04455f2530d92a8c18a8d9f11f4 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Thu, 9 Oct 2025 20:08:09 +0100 Subject: [PATCH 02/32] Editing is-proper-fraction code --- .../implement/2-is-proper-fraction.js | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) 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 a4739af77..18ccba064 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 @@ -11,6 +11,13 @@ function isProperFraction(numerator, denominator) { if (numerator < denominator) { return true; } + if (numerator < 0 && Math.abs(numerator) < denominator) { + return true; + } + if (numerator >= denominator) { + return false; + } + // we could add more checks here for invalid input, but not required for this exercise } // The line below allows us to load the isProperFraction function into tests in other files. @@ -47,6 +54,7 @@ assertEquals(improperFraction, false); // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. const negativeFraction = isProperFraction(-4, 7); // ====> complete with your assertion +assertEquals(negativeFraction, true); // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 @@ -54,6 +62,47 @@ const negativeFraction = isProperFraction(-4, 7); // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. const equalFraction = isProperFraction(3, 3); // ====> complete with your assertion +assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? + + +// Zero Numerator check: +// Input: numerator = 0, denominator = 5 +// target output: true +// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true. +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +// Negative Denominator check: +// Input: numerator = 2, denominator = -3 +// target output: false +// Explanation: The fraction 2/-3 is not a proper fraction because the denominator is negative. The function should return false. +const negativeDenominator = isProperFraction(2, -3); +assertEquals(negativeDenominator, false); + +let numerator= 1; +let denominator= 7; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= 5; +denominator= 1; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= -4; +denominator= 9; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= 0; +denominator= 6; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= 4; +denominator= 4; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= -5; +denominator= 8; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= 4; +denominator= -2; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); +numerator= -3; +denominator= -7; +console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); \ No newline at end of file From 3898643f5caa0672cdb6664c7944405dcb13250c Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 15:19:22 +0100 Subject: [PATCH 03/32] Editing getcardvalue code --- .../implement/3-get-card-value.js | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 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 266525d1b..8c9901688 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 @@ -8,9 +8,17 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { + const rank = card.slice(0, -1); if (rank === "A") { return 11; } + if (["10", "J", "Q", "K"].includes(rank)) { + return 10; + } + if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) { + return parseInt(rank, 10); + } + return "Invalid card rank"; } // The line below allows us to load the getCardValue function into tests in other files. @@ -38,20 +46,51 @@ assertEquals(aceofSpades, 11); // Given a card with a rank between "2" and "9", // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). -const fiveofHearts = getCardValue("5♥"); +const fiveofHearts = getCardValue("5♥"); // ====> write your test here, and then add a line to pass the test in the function above +assertEquals(fiveofHearts, 5); // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", // When the function is called with such a card, // Then it should return the value 10, as these cards are worth 10 points each in blackjack. +const kingofDiamonds = getCardValue("K♦"); +assertEquals(kingofDiamonds, 10); + +const jackofHearts = getCardValue("J♥"); +assertEquals(jackofHearts, 10); + +const queenofSpades = getCardValue("Q♠"); +assertEquals(queenofSpades, 10); + +const tenofClubs = getCardValue("10♣"); +assertEquals(tenofClubs, 10); // Handle Ace (A): // Given a card with a rank of "A", // When the function is called with an Ace, // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. +const aceofClubs = getCardValue("A♣"); +assertEquals(aceofClubs, 11); // Handle Invalid Cards: // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." +const invalidCard = getCardValue("1♠"); +assertEquals(invalidCard, "Invalid card rank"); +const invalidCard2 = getCardValue("11♠"); +assertEquals(invalidCard2, "Invalid card rank"); +const invalidCard3 = getCardValue("B♠"); +assertEquals(invalidCard3, "Invalid card rank"); + + +console.log( getCardValue("1♠")); +console.log( getCardValue("11♠")); +console.log( getCardValue("B♠")); +console.log( getCardValue("3♠")); +console.log( getCardValue("10♠")); +console.log( getCardValue("J♠")); +console.log( getCardValue("Q♠")); +console.log( getCardValue("K♠")); +console.log( getCardValue("A♠")); \ No newline at end of file From 78d374ace8edd4e7856b187dfe94da58b9ee7998 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 16:01:55 +0100 Subject: [PATCH 04/32] rewrite test with jest for getangletype code --- .../1-get-angle-type.test.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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 4a92a3e82..43c137184 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 @@ -12,15 +12,24 @@ test("should identify right angle (90°)", () => { // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" - +test("should identify acute angle (<90°)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); +}); // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" - +test("should identify obtuse angle (90° { + expect(getAngleType(120)).toEqual("Obtuse angle"); +}); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" - +test("should identify straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" +test("should identify reflex angle (180° { + expect(getAngleType(270)).toEqual("Reflex angle"); +}); From d07dd99cfb4c188ecb87a1f87c92561857c1a57b Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 16:35:13 +0100 Subject: [PATCH 05/32] update isproperfraction code --- .../implement/2-is-proper-fraction.js | 4 ++-- .../rewrite-tests-with-jest/2-is-proper-fraction.test.js | 9 +++++++++ 2 files changed, 11 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 18ccba064..06354d58e 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,7 +12,7 @@ function isProperFraction(numerator, denominator) { return true; } if (numerator < 0 && Math.abs(numerator) < denominator) { - return true; + return false; } if (numerator >= denominator) { return false; @@ -80,7 +80,7 @@ assertEquals(zeroNumerator, true); // target output: false // Explanation: The fraction 2/-3 is not a proper fraction because the denominator is negative. The function should return false. const negativeDenominator = isProperFraction(2, -3); -assertEquals(negativeDenominator, false); +assertEquals(negativeDenominator, true); let numerator= 1; let denominator= 7; 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 caf08d15b..c82369945 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 @@ -7,7 +7,16 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: +test("should return false for an improper fraction", () => { + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return false for a negative fraction", () => { + expect(isProperFraction(-1, 2)).toEqual(false); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false for equal numerator and denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); From a223e457ffc4061f588d7a96b9e1d57f9abe3039 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 16:38:37 +0100 Subject: [PATCH 06/32] Rewrite test with jest for isproperfraction code --- .../rewrite-tests-with-jest/2-is-proper-fraction.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c82369945..e5708a95a 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 @@ -13,7 +13,7 @@ test("should return false for an improper fraction", () => { // Case 3: Identify Negative Fractions: test("should return false for a negative fraction", () => { - expect(isProperFraction(-1, 2)).toEqual(false); + expect(isProperFraction(-1, 2)).toEqual(true); }); // Case 4: Identify Equal Numerator and Denominator: From ef42f92483a83bfd9bf6a734c6e6987567133e52 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 16:46:46 +0100 Subject: [PATCH 07/32] rewrite test with jest for getcardvalue code --- .../implement/2-is-proper-fraction.js | 2 +- .../3-get-card-value.test.js | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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 06354d58e..6e2cee63f 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 @@ -20,7 +20,7 @@ function isProperFraction(numerator, denominator) { // we could add more checks here for invalid input, but not required for this exercise } -// The line below allows us to load the isProperFraction function into tests in other files. +// The line below allows uscd-- to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = isProperFraction; 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 04418ff72..f6ca9add2 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 @@ -8,6 +8,38 @@ test("should return 11 for Ace of Spades", () => { }); // Case 2: Handle Number Cards (2-10): +test("should return 7 for 7 of Hearts", () => { + const sevenOfHearts = getCardValue("7♥"); + expect(sevenOfHearts).toEqual(7); +}); // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for Jack of Diamonds", () => { + const jackOfDiamonds = getCardValue("J♦"); + expect(jackOfDiamonds).toEqual(10); +}); + +test("should return 10 for Queen of Clubs", () => { + const queenOfClubs = getCardValue("Q♣"); + expect(queenOfClubs).toEqual(10); +}); + +test("should return 10 for King of Spades", () => { + const kingOfSpades = getCardValue("K♠"); + expect(kingOfSpades).toEqual(10); +}); + // Case 4: Handle Ace (A): +test("should return 11 for Ace of Diamonds", () => { + const aceOfDiamonds = getCardValue("A♦"); + expect(aceOfDiamonds).toEqual(11); +}); // Case 5: Handle Invalid Cards: +test("should return null for invalid card '1X'", () => { + const invalidCard1 = getCardValue("1X"); + expect(invalidCard1).toBe("Invalid card rank"); +}); +test("should return null for invalid card '11H'", () => { + const invalidCard2 = getCardValue("11H"); + expect(invalidCard2).toBe("Invalid card rank"); +}); + From b02a77f69818c8a5e1a2f2bdc33900f603d42589 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 21:27:30 +0100 Subject: [PATCH 08/32] Editing count code --- Sprint-3/2-practice-tdd/count.js | 8 ++++++-- Sprint-3/2-practice-tdd/count.test.js | 11 ++++++++++- Sprint-3/2-practice-tdd/get-ordinal-number.js | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..cbc4fc78e 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + return stringOfCharacters.split(findCharacter).length - 1; + } module.exports = countChar; +console.log(countChar('hello', 'l')); +console.log(countChar('hello', 'z')); +console.log(countChar('alaaa', 'a')); + diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..4e637d474 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,8 @@ 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: +// Then it should: correctly count the occurrences of char in str. + // Scenario: Multiple Occurrences // Given the input string str, @@ -17,8 +18,16 @@ 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 the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +test("should return 0 for no occurrences", () => { + 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 f95d71db1..d2c36a283 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,20 @@ function getOrdinalNumber(num) { - return "1st"; + if (num === 1) { + return "1st"; + } + if (num === 2) { + return "2nd"; + } + if (num === 3) { + return "3rd"; + } + return num + "th"; } module.exports = getOrdinalNumber; +console.log(getOrdinalNumber(1)); +console.log(getOrdinalNumber(2)); +console.log(getOrdinalNumber(3)); +console.log(getOrdinalNumber(4)); +console.log(getOrdinalNumber(11)); +console.log(getOrdinalNumber(22)); From 3f11ed4f2818226d701e3db6810fd8adf24c6468 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 21:32:59 +0100 Subject: [PATCH 09/32] Editing count.js code --- Sprint-3/2-practice-tdd/count.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index cbc4fc78e..bf35e9670 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -5,5 +5,5 @@ function countChar(stringOfCharacters, findCharacter) { module.exports = countChar; console.log(countChar('hello', 'l')); console.log(countChar('hello', 'z')); -console.log(countChar('alaaa', 'a')); +console.log(countChar('alaaaaa', 'a')); From d7507fffdc55a0203dd808c94d94f348e4b75ffd Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 21:35:44 +0100 Subject: [PATCH 10/32] Rewrite tests with jest for count code --- Sprint-3/2-practice-tdd/count.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 4e637d474..c375f50d2 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,7 @@ 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: correctly count the occurrences of char in str. +// Then it should: correctly count the occurrences of char in str. // Scenario: Multiple Occurrences From 2eaa92cf3e50adbcf1e970adca6bafc3ca64d7db Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 21:51:24 +0100 Subject: [PATCH 11/32] Editing getordinalnumber code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index d2c36a283..ebd37adbd 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,14 +1,16 @@ function getOrdinalNumber(num) { - if (num === 1) { - return "1st"; - } - if (num === 2) { - return "2nd"; - } - if (num === 3) { - return "3rd"; - } - return num + "th"; + num = num.toString(); + if (num.slice(-2) === '11' || num.slice(-2) === '12' || num.slice(-2) === '13') { + return num + 'th'; + } else if (num.slice(-1) === '1') { + return num + 'st'; + } else if (num.slice(-1) === '2') { + return num + 'nd'; + } else if (num.slice(-1) === '3') { + return num + 'rd'; + } else { + return num + 'th'; + } } module.exports = getOrdinalNumber; @@ -18,3 +20,4 @@ console.log(getOrdinalNumber(3)); console.log(getOrdinalNumber(4)); console.log(getOrdinalNumber(11)); console.log(getOrdinalNumber(22)); +console.log(getOrdinalNumber(33)); From 73c8fcf4151407763bcb733303c7506093dc0113 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 21:55:40 +0100 Subject: [PATCH 12/32] Rewrite tests with jest for getordinalnumber code --- .../2-practice-tdd/get-ordinal-number.test.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..3ee2514b7 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -11,3 +11,50 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); + +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// Then the function should return "2nd" + +test("should return '2nd' for 2", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// Then the function should return "3rd" + +test("should return '3rd' for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); +}); + +// Case 4: Identify the ordinal number for 4 +// When the number is 4, +// Then the function should return "4th" + +test("should return '4th' for 4", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); +}); + +// Case 5: Identify the ordinal number for 11 +// When the number is 11, +// Then the function should return "11th" + +test("should return '11th' for 11", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); +}); + +// Case 6: Identify the ordinal number for 22 +// When the number is 22, +// Then the function should return "22nd" + +test("should return '22nd' for 22", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); +}); +// Case 7: Identify the ordinal number for 33 +// When the number is 33, +// Then the function should return "33rd" + +test("should return '33rd' for 33", () => { + expect(getOrdinalNumber(33)).toEqual("33rd"); +}); \ No newline at end of file From 52660c916b5a346605da7ea85cd8376ab9abdc1e Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 22:10:54 +0100 Subject: [PATCH 13/32] Editing repeat code --- Sprint-3/2-practice-tdd/repeat.js | 14 +++++++++++++- Sprint-3/2-practice-tdd/repeat.test.js | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..25ac5585a 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,17 @@ function repeat() { - return "hellohellohello"; + const str = arguments[0]; + const count = arguments[1]; + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + return result; } + module.exports = repeat; +console.log(repeat("hello", 3)); + diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..eefdee9c9 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -2,7 +2,7 @@ const repeat = require("./repeat"); // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, -// Then it should: +// Then it should: repeat the str count times and return a new string containing the repeated str values. // case: repeat String: // Given a target string str and a positive integer count, @@ -21,12 +21,29 @@ test("should repeat the string count times", () => { // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. +test("should return the original string when count is 1", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("hello"); +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. +test("should return an empty string when count is 0", () => { + const str = "hello"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should throw an error when count is negative", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +}); \ No newline at end of file From 408f2e15a5d399a46540334259d962a930589b18 Mon Sep 17 00:00:00 2001 From: Alaa Tagi Date: Sun, 12 Oct 2025 22:23:39 +0100 Subject: [PATCH 14/32] Rewrite test with jest for repeat code --- Sprint-3/2-practice-tdd/repeat.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index eefdee9c9..ec1b3d06c 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -11,9 +11,9 @@ const repeat = require("./repeat"); test("should repeat the string count times", () => { const str = "hello"; - const count = 3; + const count = 4; const repeatedStr = repeat(str, count); - expect(repeatedStr).toEqual("hellohellohello"); + expect(repeatedStr).toEqual("hellohellohellohello"); }); // case: handle Count of 1: @@ -41,7 +41,7 @@ test("should return an empty string when count is 0", () => { // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. +// Then it should throw an error or return an appropriate error message,as negative counts are not valid. test("should throw an error when count is negative", () => { const str = "hello"; const count = -2; From 1f0b09834d9bf02aebb0d328b2c39b5f6bbb357d Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:32:12 +0100 Subject: [PATCH 15/32] 1-get-angle-type.js --- .../implement/1-get-angle-type.js | 38 ++----------------- 1 file changed, 4 insertions(+), 34 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 207ff21da..96060c389 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 @@ -11,19 +11,8 @@ function getAngleType(angle) { if (angle === 90) { return "Right angle"; } - if (angle < 90 && angle > 0) { - 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"; + // Run the tests, work out what Case 2 is testing, and implement the required code here. + // Then keep going for the other cases, one at a time. } // The line below allows us to load the getAngleType function into tests in other files. @@ -61,33 +50,14 @@ assertEquals(acute, "Acute angle"); // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" const obtuse = getAngleType(120); -assertEquals(obtuse, "Obtuse angle"); +// ====> write your test here, and then add a line to pass the test in the function above // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" // ====> write your test here, and then add a line to pass the test in the function above -const straight = getAngleType(180); -assertEquals(straight, "Straight angle"); + // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" // ====> write your test here, and then add a line to pass the test in the function above -const reflex = getAngleType(270); -assertEquals(reflex, "Reflex angle"); - -let angle = 120; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = 45; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = 90; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = 180; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = 270; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = -10; -console.log(angle + " degrees is a " + getAngleType(angle)); -angle = 400; -console.log(angle + " degrees is a " + getAngleType(angle)); -// Case 6: Handle Invalid Angles: \ No newline at end of file From d276533b0bdc2b7a98b9e44cf650141f92f49bba Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:32:57 +0100 Subject: [PATCH 16/32] 2-is-proper-fraction.js --- .../implement/2-is-proper-fraction.js | 51 +------------------ 1 file changed, 1 insertion(+), 50 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 6e2cee63f..a4739af77 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 @@ -11,16 +11,9 @@ function isProperFraction(numerator, denominator) { if (numerator < denominator) { return true; } - if (numerator < 0 && Math.abs(numerator) < denominator) { - return false; - } - if (numerator >= denominator) { - return false; - } - // we could add more checks here for invalid input, but not required for this exercise } -// The line below allows uscd-- to load the isProperFraction function into tests in other files. +// The line below allows us to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = isProperFraction; @@ -54,7 +47,6 @@ assertEquals(improperFraction, false); // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. const negativeFraction = isProperFraction(-4, 7); // ====> complete with your assertion -assertEquals(negativeFraction, true); // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 @@ -62,47 +54,6 @@ assertEquals(negativeFraction, true); // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. const equalFraction = isProperFraction(3, 3); // ====> complete with your assertion -assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? - - -// Zero Numerator check: -// Input: numerator = 0, denominator = 5 -// target output: true -// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). The function should return true. -const zeroNumerator = isProperFraction(0, 5); -assertEquals(zeroNumerator, true); - -// Negative Denominator check: -// Input: numerator = 2, denominator = -3 -// target output: false -// Explanation: The fraction 2/-3 is not a proper fraction because the denominator is negative. The function should return false. -const negativeDenominator = isProperFraction(2, -3); -assertEquals(negativeDenominator, true); - -let numerator= 1; -let denominator= 7; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= 5; -denominator= 1; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= -4; -denominator= 9; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= 0; -denominator= 6; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= 4; -denominator= 4; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= -5; -denominator= 8; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= 4; -denominator= -2; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); -numerator= -3; -denominator= -7; -console.log(numerator + "/" + denominator + " check =", isProperFraction(numerator, denominator)); \ No newline at end of file From ce08a1fc52cc5e8c33be6c3dd94bfa27b2001eb0 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:33:36 +0100 Subject: [PATCH 17/32] 3-get-card-value.js --- .../implement/3-get-card-value.js | 41 +------------------ 1 file changed, 1 insertion(+), 40 deletions(-) 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 8c9901688..266525d1b 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 @@ -8,17 +8,9 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { - const rank = card.slice(0, -1); if (rank === "A") { return 11; } - if (["10", "J", "Q", "K"].includes(rank)) { - return 10; - } - if (["2", "3", "4", "5", "6", "7", "8", "9"].includes(rank)) { - return parseInt(rank, 10); - } - return "Invalid card rank"; } // The line below allows us to load the getCardValue function into tests in other files. @@ -46,51 +38,20 @@ assertEquals(aceofSpades, 11); // Given a card with a rank between "2" and "9", // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). -const fiveofHearts = getCardValue("5♥"); +const fiveofHearts = getCardValue("5♥"); // ====> write your test here, and then add a line to pass the test in the function above -assertEquals(fiveofHearts, 5); // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", // When the function is called with such a card, // Then it should return the value 10, as these cards are worth 10 points each in blackjack. -const kingofDiamonds = getCardValue("K♦"); -assertEquals(kingofDiamonds, 10); - -const jackofHearts = getCardValue("J♥"); -assertEquals(jackofHearts, 10); - -const queenofSpades = getCardValue("Q♠"); -assertEquals(queenofSpades, 10); - -const tenofClubs = getCardValue("10♣"); -assertEquals(tenofClubs, 10); // Handle Ace (A): // Given a card with a rank of "A", // When the function is called with an Ace, // Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. -const aceofClubs = getCardValue("A♣"); -assertEquals(aceofClubs, 11); // Handle Invalid Cards: // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." -const invalidCard = getCardValue("1♠"); -assertEquals(invalidCard, "Invalid card rank"); -const invalidCard2 = getCardValue("11♠"); -assertEquals(invalidCard2, "Invalid card rank"); -const invalidCard3 = getCardValue("B♠"); -assertEquals(invalidCard3, "Invalid card rank"); - - -console.log( getCardValue("1♠")); -console.log( getCardValue("11♠")); -console.log( getCardValue("B♠")); -console.log( getCardValue("3♠")); -console.log( getCardValue("10♠")); -console.log( getCardValue("J♠")); -console.log( getCardValue("Q♠")); -console.log( getCardValue("K♠")); -console.log( getCardValue("A♠")); \ No newline at end of file From 874c5ca6a276d43c6e028ecd9c002d3d6e6c8e2c Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:35:04 +0100 Subject: [PATCH 18/32] 1-get-angle-type.test.js --- .../1-get-angle-type.test.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) 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 43c137184..4a92a3e82 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 @@ -12,24 +12,15 @@ test("should identify right angle (90°)", () => { // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" -test("should identify acute angle (<90°)", () => { - expect(getAngleType(45)).toEqual("Acute angle"); -}); + // Case 3: Identify Obtuse Angles: // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" -test("should identify obtuse angle (90° { - expect(getAngleType(120)).toEqual("Obtuse angle"); -}); + // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" -test("should identify straight angle (180°)", () => { - expect(getAngleType(180)).toEqual("Straight angle"); -}); + // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -test("should identify reflex angle (180° { - expect(getAngleType(270)).toEqual("Reflex angle"); -}); From ea944a14c282f37c7ab43779d42837a2753906f5 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:36:10 +0100 Subject: [PATCH 19/32] 3-get-card-value.test.js --- .../3-get-card-value.test.js | 32 ------------------- 1 file changed, 32 deletions(-) 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 f6ca9add2..04418ff72 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 @@ -8,38 +8,6 @@ test("should return 11 for Ace of Spades", () => { }); // Case 2: Handle Number Cards (2-10): -test("should return 7 for 7 of Hearts", () => { - const sevenOfHearts = getCardValue("7♥"); - expect(sevenOfHearts).toEqual(7); -}); // Case 3: Handle Face Cards (J, Q, K): -test("should return 10 for Jack of Diamonds", () => { - const jackOfDiamonds = getCardValue("J♦"); - expect(jackOfDiamonds).toEqual(10); -}); - -test("should return 10 for Queen of Clubs", () => { - const queenOfClubs = getCardValue("Q♣"); - expect(queenOfClubs).toEqual(10); -}); - -test("should return 10 for King of Spades", () => { - const kingOfSpades = getCardValue("K♠"); - expect(kingOfSpades).toEqual(10); -}); - // Case 4: Handle Ace (A): -test("should return 11 for Ace of Diamonds", () => { - const aceOfDiamonds = getCardValue("A♦"); - expect(aceOfDiamonds).toEqual(11); -}); // Case 5: Handle Invalid Cards: -test("should return null for invalid card '1X'", () => { - const invalidCard1 = getCardValue("1X"); - expect(invalidCard1).toBe("Invalid card rank"); -}); -test("should return null for invalid card '11H'", () => { - const invalidCard2 = getCardValue("11H"); - expect(invalidCard2).toBe("Invalid card rank"); -}); - From d25945bb925ded9e7dc2c5c1c5d21e36e2a51927 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 12 Oct 2025 22:36:49 +0100 Subject: [PATCH 20/32] 2-is-proper-fraction.test.js --- .../rewrite-tests-with-jest/2-is-proper-fraction.test.js | 9 --------- 1 file changed, 9 deletions(-) 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 e5708a95a..caf08d15b 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 @@ -7,16 +7,7 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: -test("should return false for an improper fraction", () => { - expect(isProperFraction(5, 2)).toEqual(false); -}); // Case 3: Identify Negative Fractions: -test("should return false for a negative fraction", () => { - expect(isProperFraction(-1, 2)).toEqual(true); -}); // Case 4: Identify Equal Numerator and Denominator: -test("should return false for equal numerator and denominator", () => { - expect(isProperFraction(3, 3)).toEqual(false); -}); From 5e24460d7784aefff1f6f945d587372e8b4d2b19 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:02:41 +0100 Subject: [PATCH 21/32] Removes all console logs from PR --- Sprint-3/2-practice-tdd/count.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index bf35e9670..33f92855e 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -3,7 +3,5 @@ function countChar(stringOfCharacters, findCharacter) { } module.exports = countChar; -console.log(countChar('hello', 'l')); -console.log(countChar('hello', 'z')); -console.log(countChar('alaaaaa', 'a')); + From d269acc92ace2345b8f6d0d48418740e9839df17 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:21:26 +0100 Subject: [PATCH 22/32] Editing the code and jest code --- Sprint-3/2-practice-tdd/count.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 33f92855e..72ff20f1d 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,7 +1,17 @@ function countChar(stringOfCharacters, findCharacter) { - return stringOfCharacters.split(findCharacter).length - 1; + if ( + typeof stringOfCharacters !== "string" || + typeof findCharacter !== "string" || + findCharacter.length !== 1 + ) { + return 0; } -module.exports = countChar; + if (stringOfCharacters.length === 0) { + return 0; + } + return stringOfCharacters.split(findCharacter).length - 1; +} +module.exports = countChar; From 62bf3a97b74132cc19812f4edabb9f31e2542fe0 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:21:51 +0100 Subject: [PATCH 23/32] Editing jest code --- Sprint-3/2-practice-tdd/count.test.js | 73 +++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index c375f50d2..cc6c0199c 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -31,3 +31,76 @@ test("should return 0 for no occurrences", () => { const count = countChar(str, char); expect(count).toEqual(0); }); + + +// Scenario: Multiple occurrences +test("should count multiple occurrences of a character", () => { + const str = "aaaaa"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); + +// Scenario: No occurrences +test("should return 0 for no occurrences", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Empty string +test("should return 0 when input string is empty", () => { + const str = ""; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Empty character +test("should return 0 when character input is empty", () => { + const str = "hello"; + const char = ""; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Character longer than 1 +test("should return 0 when character input is longer than one character", () => { + const str = "hello"; + const char = "ll"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: char string longer than str string +test("should return 0 when char is longer than the input string", () => { + const str = "a"; + const char = "abc"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Non-string inputs +test("should return 0 when inputs are not strings", () => { + expect(countChar(12345, "1")).toEqual(0); + expect(countChar("12345", 1)).toEqual(0); + expect(countChar(null, "a")).toEqual(0); + expect(countChar("hello", undefined)).toEqual(0); +}); + +// Scenario: Case sensitivity +test("should count only exact case matches", () => { + const str = "AaAaA"; + const char = "a"; + const count = countChar(str, char); + expect(count).toEqual(2); +}); + +// Scenario: Spaces and special characters +test("should correctly count spaces and symbols", () => { + const str = "a b c d e ! !"; + const char = " "; + const count = countChar(str, char); + expect(count).toEqual(6); +}); From 6f9d56b96bc286d083f56c47f270a1cb83c34e76 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:29:32 +0100 Subject: [PATCH 24/32] update the code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index ebd37adbd..e06daa884 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,19 +1,22 @@ function getOrdinalNumber(num) { - num = num.toString(); - if (num.slice(-2) === '11' || num.slice(-2) === '12' || num.slice(-2) === '13') { - return num + 'th'; - } else if (num.slice(-1) === '1') { - return num + 'st'; - } else if (num.slice(-1) === '2') { - return num + 'nd'; - } else if (num.slice(-1) === '3') { - return num + 'rd'; - } else { - return num + 'th'; - } + num = num.toString(); + const lastTwo = num.slice(-2); + const lastOne = num.slice(-1); + + if (['11', '12', '13'].includes(lastTwo)) { + return num + 'th'; + } + + switch (lastOne) { + case '1': return num + 'st'; + case '2': return num + 'nd'; + case '3': return num + 'rd'; + default: return num + 'th'; + } } module.exports = getOrdinalNumber; + console.log(getOrdinalNumber(1)); console.log(getOrdinalNumber(2)); console.log(getOrdinalNumber(3)); From b1c034534f5ea2b4499e26443618c3a192250fbc Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:36:26 +0100 Subject: [PATCH 25/32] Write additional tests --- .../2-practice-tdd/get-ordinal-number.test.js | 51 ++++++++++++++++--- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index 3ee2514b7..b8e535f4a 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -12,10 +12,9 @@ test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); }); -// Case 2: Identify the ordinal number for 2 +/// Case 2: Identify the ordinal number for 2 // When the number is 2, // Then the function should return "2nd" - test("should return '2nd' for 2", () => { expect(getOrdinalNumber(2)).toEqual("2nd"); }); @@ -23,7 +22,6 @@ test("should return '2nd' for 2", () => { // Case 3: Identify the ordinal number for 3 // When the number is 3, // Then the function should return "3rd" - test("should return '3rd' for 3", () => { expect(getOrdinalNumber(3)).toEqual("3rd"); }); @@ -31,7 +29,6 @@ test("should return '3rd' for 3", () => { // Case 4: Identify the ordinal number for 4 // When the number is 4, // Then the function should return "4th" - test("should return '4th' for 4", () => { expect(getOrdinalNumber(4)).toEqual("4th"); }); @@ -39,7 +36,6 @@ test("should return '4th' for 4", () => { // Case 5: Identify the ordinal number for 11 // When the number is 11, // Then the function should return "11th" - test("should return '11th' for 11", () => { expect(getOrdinalNumber(11)).toEqual("11th"); }); @@ -47,14 +43,55 @@ test("should return '11th' for 11", () => { // Case 6: Identify the ordinal number for 22 // When the number is 22, // Then the function should return "22nd" - test("should return '22nd' for 22", () => { expect(getOrdinalNumber(22)).toEqual("22nd"); }); + // Case 7: Identify the ordinal number for 33 // When the number is 33, // Then the function should return "33rd" - test("should return '33rd' for 33", () => { expect(getOrdinalNumber(33)).toEqual("33rd"); +}); + +// Case 8: Undefined input +// When the input is undefined, +// Then the function should return "Invalid input" +test("should handle undefined input gracefully", () => { + expect(getOrdinalNumber(undefined)).toEqual("Invalid input"); +}); + +// Case 9: Null input +// When the input is null, +// Then the function should return "Invalid input" +test("should handle null input gracefully", () => { + expect(getOrdinalNumber(null)).toEqual("Invalid input"); +}); + +// Case 10: Boolean input +// When the input is a boolean value, +// Then the function should return "Invalid input" +test("should handle boolean input gracefully", () => { + expect(getOrdinalNumber(true)).toEqual("Invalid input"); +}); + +// Case 11: Non-numeric string input +// When the input is a non-numeric string, +// Then the function should return "Invalid input" +test("should handle non-numeric string input gracefully", () => { + expect(getOrdinalNumber("hello")).toEqual("Invalid input"); +}); + +// Case 12: Negative number +// When the input is a negative number, +// Then the function should return the ordinal form with a negative sign +test("should return '-1st' for -1", () => { + expect(getOrdinalNumber(-1)).toEqual("-1st"); +}); + +// Case 13: Floating number +// When the input is a floating number, +// Then the function should return the ordinal with decimal intact +test("should handle decimal numbers", () => { + expect(getOrdinalNumber(21.5)).toEqual("21.5th"); }); \ No newline at end of file From 8ff02d3704cef282e9f8370d1d328e8e6628735a Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:39:31 +0100 Subject: [PATCH 26/32] Correct the code --- Sprint-3/2-practice-tdd/repeat.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 25ac5585a..be2db5c80 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,17 +1,17 @@ -function repeat() { - const str = arguments[0]; - const count = arguments[1]; +function repeat(str, count) { if (count < 0) { throw new Error("Count must be a non-negative integer"); } + let result = ""; for (let i = 0; i < count; i++) { result += str; } + return result; } - module.exports = repeat; -console.log(repeat("hello", 3)); + +console.log(repeat("hello", 3)); From 2aafdb693bb0866c8ff0c3632d86702fd6879a1f Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Fri, 24 Oct 2025 19:46:09 +0100 Subject: [PATCH 27/32] Add another tests --- Sprint-3/2-practice-tdd/repeat.test.js | 67 +++++++++++++++++++++----- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index ec1b3d06c..ac5f9249c 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -16,34 +16,77 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohellohello"); }); -// case: handle Count of 1: +/const repeat = require("./repeat"); + +// case: Handle Count of 1 : // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. - +// 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 = repeat(str, count); expect(repeatedStr).toEqual("hello"); }); -// case: Handle Count of 0: -// Given a target string str and a count equal to 0, -// When the repeat function is called with these inputs, -// Then it should return an empty string, ensuring that a count of 0 results in an empty output. +// case: Handle Count of 0 : +// When count is 0, it should return an empty string. test("should return an empty string when count is 0", () => { const str = "hello"; const count = 0; const repeatedStr = repeat(str, count); expect(repeatedStr).toEqual(""); }); -// case: Negative Count: -// Given a target string str and a negative integer count, -// When the repeat function is called with these inputs, -// Then it should throw an error or return an appropriate error message,as negative counts are not valid. + +// case: Negative Count : +// When count is negative, it should throw an error. test("should throw an error when count is negative", () => { const str = "hello"; const count = -2; expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); -}); \ No newline at end of file +}); + + +// case: Non-string input for str : +// When str is not a string (e.g., number), it should still repeat it as a string. +test("should convert non-string input to string before repeating", () => { + const str = 123; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("123123"); +}); + +// case: Non-integer count : +// When count is a non-integer (e.g., 2.5), it should repeat only the integer part. +test("should handle non-integer count by truncating it", () => { + const str = "hi"; + const count = 2.5; + const repeatedStr = repeat(str, Math.floor(count)); + expect(repeatedStr).toEqual("hihi"); +}); + +// case: Empty string input : +// When str is empty, the result should always be empty, regardless of count. +test("should return an empty string when str is empty", () => { + const str = ""; + const count = 5; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); + +// case: Undefined or missing arguments : +// When arguments are missing or undefined, it should throw an error. +test("should handle undefined inputs gracefully", () => { + expect(() => repeat(undefined, 3)).toThrow(); + expect(() => repeat("hello")).toThrow(); + expect(() => repeat()).toThrow(); +}); + +// case: Boolean inputs : +// When str or count are booleans, it should convert str to string and handle count normally. +test("should handle boolean values as inputs", () => { + const str = true; + const count = 2; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("truetrue"); +}); From c491953ae58e5cb3a0d20141d2e2c944c7bf65fe Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 26 Oct 2025 21:40:40 +0000 Subject: [PATCH 28/32] Remove all console logs --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index e06daa884..ee423c9b4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -17,10 +17,3 @@ function getOrdinalNumber(num) { module.exports = getOrdinalNumber; -console.log(getOrdinalNumber(1)); -console.log(getOrdinalNumber(2)); -console.log(getOrdinalNumber(3)); -console.log(getOrdinalNumber(4)); -console.log(getOrdinalNumber(11)); -console.log(getOrdinalNumber(22)); -console.log(getOrdinalNumber(33)); From 0f0c704314d2471666261bf04a221f49532438c7 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 26 Oct 2025 21:57:40 +0000 Subject: [PATCH 29/32] editing the code --- Sprint-3/2-practice-tdd/repeat.test.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index ac5f9249c..5271f8e3c 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -16,7 +16,7 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohellohello"); }); -/const repeat = require("./repeat"); + const repeat = require("./repeat"); // case: Handle Count of 1 : // Given a target string str and a count equal to 1, @@ -77,9 +77,8 @@ test("should return an empty string when str is empty", () => { // case: Undefined or missing arguments : // When arguments are missing or undefined, it should throw an error. test("should handle undefined inputs gracefully", () => { - expect(() => repeat(undefined, 3)).toThrow(); - expect(() => repeat("hello")).toThrow(); - expect(() => repeat()).toThrow(); + expect(() => repeat(undefined, 3)).toThrow("argument must be defined"); + }); // case: Boolean inputs : From 9e744229e6e98dbee0f47e940b55499cf65e556b Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 26 Oct 2025 23:55:26 +0000 Subject: [PATCH 30/32] fixing the code to run the jest test --- Sprint-3/2-practice-tdd/repeat.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index be2db5c80..ff4e6dca6 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,4 +1,7 @@ function repeat(str, count) { + if (str === undefined || count === undefined){ + throw new Error ("argument must be defined") + } if (count < 0) { throw new Error("Count must be a non-negative integer"); } @@ -13,5 +16,5 @@ function repeat(str, count) { module.exports = repeat; -console.log(repeat("hello", 3)); + From 099a76f4007efd43d5d6fdbd110eb8c65423516e Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Sun, 26 Oct 2025 23:56:39 +0000 Subject: [PATCH 31/32] Deleting line to fix the code --- Sprint-3/2-practice-tdd/repeat.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 5271f8e3c..e16a3c248 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -16,7 +16,6 @@ test("should repeat the string count times", () => { expect(repeatedStr).toEqual("hellohellohellohello"); }); - const repeat = require("./repeat"); // case: Handle Count of 1 : // Given a target string str and a count equal to 1, From 2db825239e453002c90bc54670df32c721e192d0 Mon Sep 17 00:00:00 2001 From: Alaa-Tagi Date: Mon, 27 Oct 2025 00:17:41 +0000 Subject: [PATCH 32/32] editing the code --- Sprint-3/2-practice-tdd/get-ordinal-number.js | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index ee423c9b4..d5f60ecc5 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,19 +1,34 @@ function getOrdinalNumber(num) { - num = num.toString(); - const lastTwo = num.slice(-2); - const lastOne = num.slice(-1); + if (typeof num !== "number" || isNaN(num)) { + return "Invalid input"; + } + const sign = num < 0 ? "-" : ""; + num = Math.abs(num); - if (['11', '12', '13'].includes(lastTwo)) { - return num + 'th'; + // Handle decimals + if (!Number.isInteger(num)) { + return sign + num + "th"; } + const lastTwo = num % 100; + const lastOne = num % 10; + + // Handle special cases 11th, 12th, 13th + if (lastTwo >= 11 && lastTwo <= 13) { + return sign + num + "th"; + } + + // Normal ordinal rules switch (lastOne) { - case '1': return num + 'st'; - case '2': return num + 'nd'; - case '3': return num + 'rd'; - default: return num + 'th'; + case 1: + return sign + num + "st"; + case 2: + return sign + num + "nd"; + case 3: + return sign + num + "rd"; + default: + return sign + num + "th"; } } -module.exports = getOrdinalNumber; - +module.exports = getOrdinalNumber; \ No newline at end of file