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..fe2463942 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 @@ -8,9 +8,12 @@ // Then, write the next test! :) Go through this process until all the cases are implemented function getAngleType(angle) { - if (angle === 90) { - return "Right angle"; - } + if (angle === 90) return "Right angle"; + if (angle < 90) return "Acute angle"; + if ((angle > 90) && (angle < 180)) return "Obtuse angle"; + if (angle === 180) return "Straight angle"; + if ((angle > 180) && (angle < 360)) return "Reflex angle"; + // 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. } @@ -50,14 +53,19 @@ 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 \ 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"); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index a4739af77..888f8bd16 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 @@ -8,15 +8,10 @@ // write one test at a time, and make it pass, build your solution up methodically function isProperFraction(numerator, denominator) { - if (numerator < denominator) { - return true; - } + if (denominator === 0) return false; // avoid division by zero + return Math.abs(numerator) < Math.abs(denominator); } -// 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; - // here's our helper again function assertEquals(actualOutput, targetOutput) { console.assert( @@ -47,6 +42,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(properFraction, true); // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 @@ -54,6 +50,9 @@ 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? + +module.exports = isProperFraction; \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index 266525d1b..841e0e6a3 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,15 +8,25 @@ // 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) { - if (rank === "A") { - return 11; - } + var rank = card.slice(0, -1); // get the rank of the card by removing the last character. (the suit is the last character) + if (rank === "A") return 11; // this checks for Aces + // Handle Number Cards (2-9) + if (rank === "2") return 2; // this checks for the twos + if (rank === "3") return 3; // this checks for the threes + if (rank === "4") return 4; // this checks for the fours + if (rank === "5") return 5; // this should check for fives + if (rank === "6") return 6; // this checks for the sixes + if (rank === "7") return 7; // this checks for the sevens + if (rank === "8") return 8; // this checks for the eights + if (rank === "9") return 9; // this checks for the nines + // Handle Face Cards (J, Q, K) And 10's + if (rank === "J") return 10; // this checks for Jacks + if (rank === "Q") return 10; // this checks for Queens + if (rank === "K") return 10; // this checks for Kings + if (rank === "10") return 10; // this checks for Tens + // if none of the above its an invalid card and throw an error + throw new Error("Invalid card rank."); // this will throw an error if the card is not a valid rank } - -// The line below allows us to load the getCardValue function into tests in other files. -// This will be useful in the "rewrite tests with jest" step. -module.exports = getCardValue; - // You need to write assertions for your function to check it works in different cases // we're going to use this helper function to make our assertions easier to read // if the actual output matches the target output, the test will pass @@ -38,16 +48,27 @@ 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♥"); // ====> write your test here, and then add a line to pass the test in the function above +const fiveofHearts = getCardValue("5♥"); +const sixofDiamonds = getCardValue("6♦"); +const sevenofClubs = getCardValue("7♣"); +const eightofSpades = getCardValue("8♠"); +assertEquals(fiveofHearts, 5); +assertEquals(sixofDiamonds, 6); +assertEquals(sevenofClubs, 7); +assertEquals(eightofSpades, 8); // 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 jackOfDiamonds = getCardValue("J♦"); +const queenOfClubs = getCardValue("Q♣"); +const kingOfSpades = getCardValue("K♠"); +assertEquals(jackOfDiamonds, 10); +assertEquals(queenOfClubs, 10); +assertEquals(kingOfSpades, 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. @@ -55,3 +76,10 @@ const fiveofHearts = getCardValue("5♥"); // 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." +try { + getCardValue("z♠"); // this should throw an error of "Invalid card rank." + console.log("Test failed: Expected an error for invalid card rank."); +} catch (error) { + assertEquals(error.message, "Invalid card rank."); +} +module.exports = getCardValue; 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..2f58e3121 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,27 @@ 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 as Acute angle (< 90°)", () => { + expect(getAngleType(75)).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 as Obtuse angle (> 90° and < 180°)", () => { + expect(getAngleType(155)).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 as 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 as Reflex angle (> 180° and < 360°)", () => { + expect(getAngleType(270)).toEqual("Reflex angle"); +}); diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index caf08d15b..f62baa0a2 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,21 @@ 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 true for a negative proper fraction", () => { + expect(isproperFraction(-4, 7)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test ("should return false when numerator equals denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); + +// Case 5: Identify Negative Improper Fractions: +test("should return false for a negative improper fraction", () => { + expect(isProperFraction(-4, 4)).toEqual(false); + expect(isProperFraction(10, -3)).toEqual(false); 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..b2fa048f3 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 @@ -7,7 +7,29 @@ test("should return 11 for Ace of Spades", () => { expect(aceofSpades).toEqual(11); }); -// Case 2: Handle Number Cards (2-10): +test("should return the correct value for number cards between 2 and 9", () => { + expect(getCardValue("2♥")).toEqual(2); + expect(getCardValue("3♦")).toEqual(3); + expect(getCardValue("4♣")).toEqual(4); + expect(getCardValue("5♠")).toEqual(5); + expect(getCardValue("6♥")).toEqual(6); + expect(getCardValue("7♦")).toEqual(7); + expect(getCardValue("8♣")).toEqual(8); + expect(getCardValue("9♠")).toEqual(9); + expect(getCardValue("10♥")).toEqual(10); +}); // Case 3: Handle Face Cards (J, Q, K): +test("should return 10 for face cards (J, Q, K)", () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♣")).toEqual(10); +}); // Case 4: Handle Ace (A): +test("should return 11 for Aces", () => { + expect(getCardValue("A♠")).toEqual(11); +}); // Case 5: Handle Invalid Cards: +test("should throw an error for invalid card ranks", () => { + expect(() => getCardValue("X♠")).toThrow("Invalid card rank."); + expect(() => getCardValue("1♠")).toThrow("Invalid card rank."); +}); diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..f18e4ebd1 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,18 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + // start a count of 0 + let count = 0; + + // check each of the characters in the string one by one. + for (let i = 0; i < stringOfCharacters.length; i++) { + // checks if the current characters matches the one were looking for in the string. + if (stringOfCharacters[i] === findCharacter) + // if it does, we increment the count by 1. + count = count + 1; +} + + return count; } +console.log(countChar("aaaaa", "a")); // 5 +console.log(countChar("hello", "l")); // 2 -module.exports = countChar; +module.exports = countChar; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..9f0bf3d7d 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => { // 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 of a character", () => { + const str = "example"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db1..a6ba0d68b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,26 @@ function getOrdinalNumber(num) { - return "1st"; + const lastTwoDigits = num % 100; // gets the last two digits of the number because some like 11, 12, 13 are special cases. + const lastDigit= num % 10; // gets the last digit to decide if its going to be "St, Nd, Rd" + + // handles special cases like "11,12,13" to always end in the "Th" + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13){ + return num + "th"; +} + // will return "St" if the number ends in 1. + if (lastDigit === 1){ + return num + "st"; + } +// will return "Nd" if the number ends in 2. + if (lastDigit === 2){ + return num + "nd"; + } +// will return "Rd" if the number ends in 3. + if (lastDigit === 3){ + return num + "rd"; + } + +// will return all numbers that end in 4, 5, 6, 7, 8, 9 with "Th". + return num + "th"; } module.exports = getOrdinalNumber; 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..a5114020c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -10,4 +10,37 @@ const getOrdinalNumber = require("./get-ordinal-number"); test("should return '1st' for 1", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st") + expect(getOrdinalNumber(141)).toEqual("141st") +}); +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// The function should then return "2nd". + +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// The Function should the return "3rd" + +test("Should return `3rd` for 3", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(133)).toEqual("133rd"); +}); +// Case 4: identify the special ordinal numbers for 11, 12, 13 +// When the number is 11, 12, 13, +// The function should return "11th, 12th, 13th" + +test("should return `11th, 12th, 13th` for special ordinal numbers ending on these", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(111)).toEqual("111th"); + expect(getOrdinalNumber(112)).toEqual("112th"); + expect(getOrdinalNumber(113)).toEqual("113th"); }); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..a3262d8f9 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,19 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str, count) { + // check if "count" is a negative number if so will throw new error + if (count < 0) { + throw new error("Count must be a positive number"); + + //check if the is equal to zero + if (count === 0){ + return ""; // returns empty string if count is equal to zero + } + //check if the count is equals 1. + if (count === 1) { + //returns just the string as its not needed to be repeated + return str; + } + return str.repeat(count); // if the count is above two it repeat "count" number of times. +} } 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 fc59d019e..bf461b026 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,30 @@ test("should repeat the string count times", () => { // 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, 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 repeatStr 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 give a 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 repeatStr 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 = -1; + expect(() => repeat(str,count)).toThrow("Count must be a positive number"); +}); \ No newline at end of file