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..78c1509de 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -1,56 +1,44 @@ -// Implement a function getAngleType -// Build up your function case by case, writing tests as you go -// The first test and case is written for you. The next case has a test, but no code. -// Execute this script in your terminal -// node 1-get-angle-type.js -// The assertion error will tell you what the expected output is -// Write the code to pass the test -// Then, write the next test! :) Go through this process until all the cases are implemented - function getAngleType(angle) { if (angle === 90) return "Right angle"; - // read to the end, complete line 36, then pass your test here -} + 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" -// 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 -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); + // read to the end, complete line 36, then pass your test here } + // Acceptance criteria: - // Given an angle in degrees, // When the function getAngleType is called with this angle, // Then it should: - // Case 1: Identify Right Angles: // When the angle is exactly 90 degrees, // Then the function should return "Right angle" const right = getAngleType(90); assertEquals(right, "Right angle"); - // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" const acute = getAngleType(45); assertEquals(acute, "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" 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 straightangle = getAngleType(180) +assertEquals(straightangle, "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 reflexangle = getAngleType(250) +assertEquals(reflexangle, "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..6b3098a42 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -40,6 +40,7 @@ assertEquals(improperFraction, false); // target output: true // 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); +assertEquals(negativeFraction, true); // ====> complete with your assertion // Equal Numerator and Denominator check: @@ -47,7 +48,8 @@ const negativeFraction = isProperFraction(-4, 7); // target output: false // 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); +assertEquals(equalFraction, false); // ====> complete with your assertion // Stretch: -// What other scenarios could you test for? +// What other scenarios could you test for? \ No newline at end of file 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..35432d7cc 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -1,3 +1,4 @@ + // This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck // You will need to implement a function getCardValue @@ -7,10 +8,18 @@ // complete the rest of the tests and cases // 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; -} + function getCardValue(card) { + const rankChar = card.slice(0, -1); // Remove the last character (suit) + const rankInt = parseInt(rankChar); // Try to convert rank to a number + + if (rankChar === "A") return 11; // Ace is worth 11 + else if (["K", "Q", "J"].includes(rankChar)) return 10; // Face cards worth 10 + else if (rankInt >= 2 && rankInt <= 10) return rankInt; // 2–10 cards + else throw new Error("Invalid card 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 @@ -30,22 +39,38 @@ assertEquals(aceofSpades, 11); // Handle Number Cards (2-10): // Given a card with a rank between "2" and "9", -// When the function is called with such a card, +// 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♥"); +assertEquals(fiveofHearts, 5) +const sixofHearts = getCardValue("6♥"); +assertEquals(sixofHearts, 6) + // ====> write your test here, and then add a line to pass the test in the function above // 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 facecards = getCardValue("Q♥") +assertEquals(facecards, 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 ace = getCardValue("A♥") +assertEquals(ace, 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." +function assertThrowsInvalidCard(fn) { + try { + fn(); + console.assert(false, "Expected 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..f771193a6 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,11 @@ -function getAngleType(angle) { +function getAngleType(angle) { + if (angle <= 0 || angle >= 360) return "Invalid 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"; + return "Invalid angle"; } @@ -10,7 +14,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..b220deb6d 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 @@ -1,10 +1,59 @@ const getAngleType = require("./1-get-angle-type"); -test("should identify right angle (90°)", () => { + /*test("should identify right angle (90°)", () => { + expect(getAngleType(90)).toEqual("Right angle");}); + + test("should identify acute angle (45°)", () => { + expect(getAngleType(45)).toEqual("Acute angle");}); + + test("should identify obtuse angle (120°)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle");}); + + test("should identify straight angle (180°)", () => { + expect(getAngleType(180)).toEqual("Straight angle");}); + + test("should identify reflex angle (220°)", () => { + expect(getAngleType(220)).toEqual("Reflex angle");}); + + */ + test("should identify right angle (angle === 90)", () => { expect(getAngleType(90)).toEqual("Right angle"); }); -// REPLACE the comments with the tests +test("should identify acute angle (angle < 90)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(30)).toEqual("Acute angle"); + expect(getAngleType(0.1)).toEqual("Acute angle"); +}); + +test("should identify obtuse angle (90 < angle < 180)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179.9)).toEqual("Obtuse angle"); +}); + +test("should identify straight angle (angle === 180)", () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + +test("should identify reflex angle (180 < angle < 360)", () => { + expect(getAngleType(220)).toEqual("Reflex angle"); + expect(getAngleType(300)).toEqual("Reflex angle"); + expect(getAngleType(359.999)).toEqual("Reflex angle"); + expect(getAngleType(180.001)).toEqual("Reflex angle"); +}); + +test("should return 'Invalid angle' for invalid inputs", () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(-45)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(400)).toEqual("Invalid angle"); + expect(getAngleType("90")).toEqual("Invalid angle"); + expect(getAngleType(NaN)).toEqual("Invalid angle"); +}); + + + // make your test descriptions as clear and readable as possible // Case 2: Identify Acute Angles: @@ -21,4 +70,4 @@ test("should identify right angle (90°)", () => { // 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" +// Then the function should return "Reflex angle" \ No newline at end of file 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..a06524575 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 + + if (denominator === 0) throw new Error("Denominator cannot be zero"); + return Math.abs(numerator) < Math.abs(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..74f80d565 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 @@ -3,9 +3,13 @@ const isProperFraction = require("./2-is-proper-fraction"); test("should return true for a proper fraction", () => { expect(isProperFraction(2, 3)).toEqual(true); }); +test("should return false for a proper fraction", () => { + expect(isProperFraction(5, 3)).toEqual(false); +}); -// Case 2: Identify Improper Fractions: - -// Case 3: Identify Negative Fractions: - -// Case 4: Identify Equal Numerator and Denominator: +test("should return true for a negative fraction", () => { + expect(isProperFraction(-4, -6)).toEqual(true); +}); +test("should return false for a equal fraction", () => { + expect(isProperFraction(3, 3)).toEqual(false); +}); \ No newline at end of file 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..5c26358a5 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,15 @@ function getCardValue(card) { - // replace with your code from key-implement - return 11; + const rankChar = card.slice(0, -1) + + if (rankChar === "A") return 11; //If rank is 'A' (Ace), return 11 points + if (["K", "Q", "J"].includes(rankChar)) return 10; //// If rank is 'K', 'Q', or 'J' (King, Queen, Jack), return 10 points + + const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; + if (validRanks.includes(rankChar)) return Number(rankChar); // Define all valid numeric ranks as strings + + throw new Error("Invalid card rank.");//If none of the above conditions match, the card rank is invalid, so throw an error } -module.exports = getCardValue; \ No newline at end of file + + +module.exports = getCardValue; + 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..b81192c09 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 @@ -1,11 +1,34 @@ const getCardValue = require("./3-get-card-value"); -test("should return 11 for Ace of Spades", () => { +test("should return 11 for Ace of Spades", () => { const aceofSpades = getCardValue("A♠"); expect(aceofSpades).toEqual(11); }); + + +test("should return rank for Number Cards", () => { + const fiveofHearts = getCardValue("5♥"); + expect(fiveofHearts).toEqual(5); + }); + +test("should return 10 for Face Cards", () => { + const facecards = getCardValue("Q♥"); + expect(facecards).toEqual(10); + }); + +test("should return 11 for AceCard", () => { + const AceCard = getCardValue("A♥"); + expect(AceCard).toEqual(11); + }); + +test('throws error for invalid card', () => { + expect(() => getCardValue("ST")).toThrow("Invalid card rank."); + expect(() => getCardValue("0x02♠")).toThrow("Invalid card rank."); + expect(() => getCardValue("5.1♠")).toThrow("Invalid card rank."); + expect(() => getCardValue(" 5 ♠")).toThrow("Invalid card rank."); +}); // Case 2: Handle Number Cards (2-10): // Case 3: Handle Face Cards (J, Q, K): // Case 4: Handle Ace (A): -// Case 5: Handle Invalid Cards: +// Case 5: Handle Invalid Cards: \ No newline at end of file diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..4e2ee5d17 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -1,5 +1,15 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + if (!stringOfCharacters.includes(findCharacter)) + return 0; + else { + let count = 0; + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; + } } 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..ef3b3a598 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.test.js +++ b/Sprint-3/3-mandatory-practice/implement/count.test.js @@ -10,15 +10,31 @@ const countChar = require("./count"); // When the function is called with these inputs, // Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa'). -test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; +/*test("should count multiple occurrences of a character", () => { + const str = "aaaaaa"; const char = "a"; const count = countChar(str, char); - expect(count).toEqual(5); + expect(count).toEqual(6); +}); +test("should count multiple occurrences of a character", () => { + const str = "sftyeh"; + const char = "s"; + const count = countChar(str, char); + expect(count).toEqual(1); +});*/ +test("should count occurrences of a character", () => { + expect(countChar("aaaaaa", "a")).toEqual(6); + expect(countChar("sftyeh", "s")).toEqual(1); }); - // 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 zero for no occurrences of a character", () =>{ + const str1= "aaaaa"; + const char1 = "s"; + const count1 = countChar(str1, char1); + expect(count1).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..9377a58fe 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,16 @@ function getOrdinalNumber(num) { - return "1st"; + const remainder10 = num % 10; + const remainder100 = num % 100; + + if (remainder100 >= 11 && remainder100 <= 13) { //deals with numbers 11,12,13 + return `${num}th`; //using template literal to combine num with a string th. + } + + if (remainder10 === 1) return `${num}st`; + if (remainder10 === 2) return `${num}nd`; + if (remainder10 === 3) return `${num}rd`; + + 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..60df9a17a 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 @@ -8,6 +8,30 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { - expect(getOrdinalNumber(1)).toEqual("1st"); - }); +test("append 'st' to numbers ending in 1, except those ending in 11", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(11)).toEqual("11th"); // exception case +}); + +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(12)).toEqual("12th"); // exception case +}); + +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(203)).toEqual("203rd"); + expect(getOrdinalNumber(13)).toEqual("13th"); // exception case +}); + +test("append 'th' to numbers ending in 0, 4–9, or 11–13", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + expect(getOrdinalNumber(28)).toEqual("28th"); + expect(getOrdinalNumber(100)).toEqual("100th"); +}); \ 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..e4db1121d 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.js @@ -1,5 +1,9 @@ -function repeat() { - return "hellohellohello"; +function repeat(str, count) { + if (count < 0) { + throw new Error("Invalid input: count must be non-negative"); + } + return String(str).repeat(count); } + 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..4da93305c 100644 --- a/Sprint-3/3-mandatory-practice/implement/repeat.test.js +++ b/Sprint-3/3-mandatory-practice/implement/repeat.test.js @@ -20,13 +20,26 @@ test("should repeat the string count times", () => { // 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. - +test("should repeat the string count times", () => { + 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", () => { + 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 for negative count", () => { + expect(() => repeat("hello", -2)).toThrow("Invalid input: count must be non-negative"); +}); \ No newline at end of file