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..bf21ce2b5 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 @@ -50,14 +50,16 @@ 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 +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..eb15bc2bb 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 @@ -46,14 +46,31 @@ 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); -// ====> complete with your assertion +assertEquals(negativeFraction, true); // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 // 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); -// ====> complete with your assertion +assertEquals(equalFraction, false); // Stretch: // What other scenarios could you test for? + + +// // Stretch Scenarios: + +// Stretch 1: Zero in numerator +// 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). Any fraction with zero numerator is proper (except when denominator is also zero). +const zeroNumerator = isProperFraction(0, 5); +assertEquals(zeroNumerator, true); + +// Stretch 2: Negative denominator +// Input: numerator = 2, denominator = -5 +// target output: false +// Explanation: The fraction 2/-5 equals -0.4, but when comparing 2 < -5, this is false. The function compares the actual values, not absolute values. +const negativeDenominator = isProperFraction(2, -5); +assertEquals(negativeDenominator, false); \ 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..4150de844 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,23 @@ // 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) { + // Handle "10" case first since it has two characters + if (card.startsWith("10")) { + return 10; + } + + const rank = card[0]; // Extract the rank from the card string if (rank === "A") { return 11; } + if (rank >= "2" && rank <= "9") { + return parseInt(rank); + } + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + + throw new Error("Invalid card rank."); } // The line below allows us to load the getCardValue function into tests in other files. @@ -26,6 +40,7 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } + // Acceptance criteria: // Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A), @@ -39,19 +54,38 @@ assertEquals(aceofSpades, 11); // 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 +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 jackOfDiamonds = getCardValue("J♦"); +assertEquals(jackOfDiamonds, 10); + +const queenOfHearts = getCardValue("Q♥"); +assertEquals(queenOfHearts, 10); + +const kingOfClubs = getCardValue("K♣"); +assertEquals(kingOfClubs, 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 aceOfDiamonds = getCardValue("A♦"); +assertEquals(aceOfDiamonds, 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." +try { + getCardValue("X♠"); + console.assert(false, "Expected an error for invalid card"); +} catch (error) { + console.assert(error.message === "Invalid card rank.", "Expected invalid card rank error"); +} \ No newline at end of file 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..3fcea5d91 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 @@ -1,26 +1,41 @@ // This statement loads the getAngleType function you wrote in the implement directory. // We will use the same function, but write tests for it using Jest in this file. -const getAngleType = require("../implement/1-get-angle-type"); +const getAngleType = require('../implement/1-get-angle-type'); -test("should identify right angle (90°)", () => { +test("should identify right angle (90)", () => { expect(getAngleType(90)).toEqual("Right angle"); }); -// REPLACE the comments with the tests -// make your test descriptions as clear and readable as possible - // Case 2: Identify Acute Angles: // When the angle is less than 90 degrees, // Then the function should return "Acute angle" +test("should identify acute angles (less than 90 degrees)", () => { + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(30)).toEqual("Acute angle"); + expect(getAngleType(89)).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 angles (between 90 and 180 degrees)", () => { + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(179)).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 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 reflex angles (between 180 and 360 degrees)", () => { + expect(getAngleType(200)).toEqual("Reflex angle"); + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); \ No newline at end of file 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..766a56083 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,20 @@ test("should return true for a proper fraction", () => { }); // Case 2: Identify Improper Fractions: +test("should return false for improper fractions", () => { + expect(isProperFraction(5, 2)).toEqual(false); + expect(isProperFraction(10, 5)).toEqual(false); +}); // Case 3: Identify Negative Fractions: +test("should handle negative fractions correctly", () => { + expect(isProperFraction(-4, 7)).toEqual(true); + expect(isProperFraction(2, -5)).toEqual(false); + expect(isProperFraction(-2, -5)).toEqual(false); +}); // Case 4: Identify Equal Numerator and Denominator: +test("should return false when numerator equals denominator", () => { + expect(isProperFraction(3, 3)).toEqual(false); + expect(isProperFraction(7, 7)).toEqual(false); +}); \ No newline at end of file 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..e92d95a1d 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,28 @@ test("should return 11 for Ace of Spades", () => { }); // Case 2: Handle Number Cards (2-10): +test("should return numeric value for number cards (2-9)", () => { + expect(getCardValue('5♥')).toEqual(5); + expect(getCardValue('2♦')).toEqual(2); + expect(getCardValue('9♣')).toEqual(9); +}); + // 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 all Aces", () => { + expect(getCardValue('A♦')).toEqual(11); + expect(getCardValue('A♥')).toEqual(11); + expect(getCardValue('A♣')).toEqual(11); +}); + // Case 5: Handle Invalid Cards: +test("should throw error for invalid card ranks", () => { + expect(() => getCardValue('X♠')).toThrow('Invalid card rank.'); + expect(() => getCardValue('1♥')).toThrow('Invalid card rank.'); +}); \ 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..511407a3b 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -1,15 +1,15 @@ // implement a function countChar that counts the number of times a character occurs in a string -const countChar = require("./count"); +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: -// Scenario: Multiple Occurrences +// Scenario: Multiple occurrences // Given the input string str, // And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'), // 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"; const char = "a"; @@ -17,8 +17,50 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences +// 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 when character is not found", () => { + const str = "hello world"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +// Scenario: Case sensitivity +// Given the input string str with mixed case, +// And a character char with specific case, +// When the function is called with these inputs, +// Then it should only count occurrences that match the case exactly. +test("should be case sensitive when counting characters", () => { + const str = "Hello World"; + const char = "h"; + const count = countChar(str, char); + expect(count).toEqual(0); // 'h' is not the same as 'H' +}); + +// Scenario: Single occurrence +// Given the input string str, +// And a character char that appears exactly once, +// When the function is called with these inputs, +// Then it should return 1. +test("should count single occurrence correctly", () => { + const str = "hello"; + const char = "e"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +// Scenario: Empty string +// Given an empty input string str, +// And any character char, +// When the function is called with these inputs, +// Then it should return 0. +test("should return 0 for empty string", () => { + const str = ""; + const char = "a"; + 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.test.js b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js index dfe4b6091..913ff6c22 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -1,13 +1,74 @@ -const getOrdinalNumber = require("./get-ordinal-number"); -// In this week's prep, we started implementing getOrdinalNumber +const getOrdinalNumber = require('./get-ordinal-number'); +// In this week's prep, we started implementing getOrdinalNumber // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback // Case 1: Identify the ordinal number for 1 // When the number is 1, // Then the function should return "1st" - 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 numbers ending with 1 (except 11) +// When the number ends with 1 but is not 11, +// Then the function should return "[number]st" +test("should return correct ordinal for numbers ending with 1 (except 11)", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(31)).toEqual("31st"); + expect(getOrdinalNumber(41)).toEqual("41st"); +}); + +// Case 5: Identify the ordinal number for numbers ending with 2 (except 12) +// When the number ends with 2 but is not 12, +// Then the function should return "[number]nd" +test("should return correct ordinal for numbers ending with 2 (except 12)", () => { + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(32)).toEqual("32nd"); + expect(getOrdinalNumber(42)).toEqual("42nd"); +}); + +// Case 6: Identify the ordinal number for numbers ending with 3 (except 13) +// When the number ends with 3 but is not 13, +// Then the function should return "[number]rd" +test("should return correct ordinal for numbers ending with 3 (except 13)", () => { + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(43)).toEqual("43rd"); +}); + +// Case 7: Identify the ordinal number for numbers ending with 11, 12, 13 +// When the number is 11, 12, or 13, +// Then the function should return "[number]th" +test("should return 'th' for numbers 11, 12, 13", () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); + +// Case 8: Identify the ordinal number for other numbers +// When the number doesn't end with 1, 2, or 3 (or is 11, 12, 13), +// Then the function should return "[number]th" +test("should return 'th' for other numbers", () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(5)).toEqual("5th"); + expect(getOrdinalNumber(10)).toEqual("10th"); + expect(getOrdinalNumber(15)).toEqual("15th"); + expect(getOrdinalNumber(20)).toEqual("20th"); + expect(getOrdinalNumber(100)).toEqual("100th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..0066d0ec0 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.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 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 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 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 error for negative count", () => { + const str = "hello"; + const count = -2; + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +}); \ No newline at end of file diff --git a/Sprint-3/3-stretch/find.js b/Sprint-3/3-stretch/find.js index c7e79a2f2..933d6bb7f 100644 --- a/Sprint-3/3-stretch/find.js +++ b/Sprint-3/3-stretch/find.js @@ -20,6 +20,20 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// The variable index starts at 0. +// Each time the loop runs and the character is not found, index increases by 1 because of index++. +// This continues until: +// The character is found → return index +// OR the end of the string is reached → return -1 + // b) What is the if statement used to check +// This checks whether the character at the current position in the string matches the character we are searching for. +// If it matches, it returns the current index + // c) Why is index++ being used? +// index++ increases the value of index by 1 after each loop iteration. +// This allows the code to check the next character in the string on the next loop run + // d) What is the condition index < str.length used for? +// This makes sure the loop stops when we reach the end of the string. +// It prevents accessing str[index] when index is out of range \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.js b/Sprint-3/3-stretch/password-validator.js index b55d527db..b3540d62f 100644 --- a/Sprint-3/3-stretch/password-validator.js +++ b/Sprint-3/3-stretch/password-validator.js @@ -1,6 +1,20 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +function isValidPassword(password) { + // Check minimum length + if (password.length < 5) return false; + + // Check for at least one uppercase letter + if (!/[A-Z]/.test(password)) return false; + + // Check for at least one lowercase letter + if (!/[a-z]/.test(password)) return false; + + // Check for at least one number + if (!/[0-9]/.test(password)) return false; + + // Check for at least one special character + if (!/[!#$%.*&]/.test(password)) return false; + + return true; } - -module.exports = passwordValidator; \ No newline at end of file +module.exports = isValidPassword; \ No newline at end of file diff --git a/Sprint-3/3-stretch/password-validator.test.js b/Sprint-3/3-stretch/password-validator.test.js index 8fa3089d6..d27d6d9c8 100644 --- a/Sprint-3/3-stretch/password-validator.test.js +++ b/Sprint-3/3-stretch/password-validator.test.js @@ -15,12 +15,42 @@ To be valid, a password must: You must breakdown this problem in order to solve it. Find one test case first and get that working */ const isValidPassword = require("./password-validator"); + test("password has at least 5 characters", () => { // Arrange - const password = "12345"; + const password = "Ab1!c"; // Act const result = isValidPassword(password); // Assert expect(result).toEqual(true); -} -); \ No newline at end of file +}); + +// Test minimum length +test("should return false for password with less than 5 characters", () => { + expect(isValidPassword("Ab1!")).toEqual(false); +}); + +// Test uppercase requirement +test("should return false for password without uppercase", () => { + expect(isValidPassword("abc12!")).toEqual(false); +}); + +// Test lowercase requirement +test("should return false for password without lowercase", () => { + expect(isValidPassword("ABC12!")).toEqual(false); +}); + +// Test number requirement +test("should return false for password without number", () => { + expect(isValidPassword("Abcde!")).toEqual(false); +}); + +// Test special character requirement +test("should return false for password without special character", () => { + expect(isValidPassword("Abcde1")).toEqual(false); +}); + +// Test valid password +test("should return true for valid password", () => { + expect(isValidPassword("Valid1!")).toEqual(true); +}); \ No newline at end of file