diff --git a/Sprint-3/1-key-implement/1-get-angle-type.js b/Sprint-3/1-key-implement/1-get-angle-type.js index 08d1f0cba..bcad03b92 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -9,6 +9,10 @@ function getAngleType(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"; // read to the end, complete line 36, then pass your test here } @@ -43,14 +47,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-key-implement/2-is-proper-fraction.js b/Sprint-3/1-key-implement/2-is-proper-fraction.js index 91583e941..7df83dfb4 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -9,6 +9,7 @@ function isProperFraction(numerator, denominator) { if (numerator < denominator) return true; + else return false; } // here's our helper again @@ -41,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 @@ -48,6 +50,7 @@ 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? diff --git a/Sprint-3/1-key-implement/3-get-card-value.js b/Sprint-3/1-key-implement/3-get-card-value.js index aa1cc9f90..a4dc4ede8 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -8,9 +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 } - // 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 @@ -32,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. @@ -49,3 +76,9 @@ 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."); +} \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js index d61254bd7..03b38b79f 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.js @@ -1,7 +1,14 @@ function getAngleType(angle) { if (angle === 90) return "Right angle"; // replace with your completed function from key-implement - + 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"; } @@ -10,7 +17,6 @@ function getAngleType(angle) { - // Don't get bogged down in this detail // Jest uses CommonJS module syntax by default as it's quite old // We will upgrade our approach to ES6 modules in the next course module, so for now diff --git a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js index b62827b7c..a5d7708a5 100644 --- a/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js +++ b/Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js @@ -10,15 +10,31 @@ 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 an acute angle (less than 90 degrees)", () => { + 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 as an obtuse angle (greater then 90 degrees and less then 180 degrees)", () => { + expect(getAngleType(90.38)).toEqual("Obtuse angle"); + expect(getAngleType(179.99)).toEqual("Obtuse angle"); + expect(getAngleType(139)).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 a straight angle (exactly 180 degrees)", () => { + 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 a reflex angle (greater than 180 degrees and less than 360 degrees)", () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(359.99)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); +}); diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js index 9836fe398..175849a78 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js @@ -1,6 +1,6 @@ function isProperFraction(numerator, denominator) { if (numerator < denominator) return true; - // add your completed function from key-implement here + else return false; // returns false if numerator is not less than denominator } module.exports = isProperFraction; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js index ff1cc8173..979f0f904 100644 --- a/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js +++ b/Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js @@ -5,7 +5,16 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: +test("should return false for a improper faction", () => { + expect(isProperFraction(5, 2)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should return true for a negative fraction", () => { + expect(isProperFraction(-4, 7)).toEqual(true); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false for a equal fraction", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}) diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js index 0d95d3736..5eb2ebe79 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.js @@ -1,5 +1,22 @@ function getCardValue(card) { - // replace with your code from key-implement - 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 } + module.exports = getCardValue; \ No newline at end of file diff --git a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js index 03a8e2f34..1aeba3e40 100644 --- a/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js +++ b/Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js @@ -6,6 +6,29 @@ test("should return 11 for Ace of Spades", () => { }); // 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/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..f18e4ebd1 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/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; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/count.test.js b/Sprint-3/3-mandatory-practice/implement/count.test.js index 42baf4b4b..9f0bf3d7d 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/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/3-mandatory-practice/implement/get-ordinal-number.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js index 24f528b0d..c9b9946a9 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js @@ -1,5 +1,27 @@ 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 "11, 12 ,13" to always end in "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; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js index 6d55dfbb4..b116fdd3a 100644 --- a/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js +++ b/Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js @@ -6,8 +6,43 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Case 1: Identify the ordinal number for 1 // When the number is 1, -// Then the function should return "1st" +// Then the function should return ordinal numbers what end with the "1st" -test("should return '1st' for 1", () => { +test("should return '1st' for ordinal numbers ending in 1 like (1, 21, 31..)", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(31)).toEqual("31st") }); + +// Case 2: Identify the ordinal number for 2 +// When the number is 2, +// The function should then return ordinal numbers what end with the "2nd". + +test("Should return `2nd` for ordinal numbers ending in 2 like (2, 22, 32...)", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); +}); + +// Case 3: Identify the ordinal number for 3 +// When the number is 3, +// The Function should return the ordinal numbers what finish with the "3rd" + +test("Should return `3rd` for ordinal numbers ending in 3 like (3, 23, 33...", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); +}); + +// 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"); +}); \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.js b/Sprint-3/3-mandatory-practice/implement/repeat.js index 621f9bd35..4ee8bc605 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,25 @@ -function repeat() { - return "hellohellohello"; -} +function repeat(str, count) { + // Check if "count" is a negative number. + if (count < 0) { + // Will throw error if "count" is negative. + throw new Error("Count must be a positive number"); + } + // Check if the count equals 0 + if (count === 0) { + // Returns empty string if count is equal to 0. + return ""; + } + //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. +} +/* console.log(repeat("hello", 0)); // expect "" +console.log(repeat("hello", 1)); // expect string to be called +console.log(repeat("hello", 3));// expect string to be repeated 3 times. +console.log(repeat("hello", -1)); // expect string to throw error for being a negative number +*/ module.exports = repeat; \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/repeat.test.js b/Sprint-3/3-mandatory-practice/implement/repeat.test.js index 8a4ab42ef..bd21f0223 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -21,12 +21,32 @@ 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 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 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 = -1; + expect(() => repeat(str,count)).toThrow("Count must be a positive number"); +}); \ No newline at end of file