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 9e05a871e2..4297d51c25 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 @@ -16,8 +16,28 @@ function getAngleType(angle) { // TODO: Implement this function + if (angle > 0 && angle < 90){ + return "Acute angle"; } + else if (angle === 90 ){ + return "Right angle"; + } + else if (angle > 90 && angle < 180){ + return "Obtuse angle"; + } + else if (angle === 180){ + return "Straight angle"; + } + else if (angle > 180 && angle < 360){ + return "Reflex angle"; + } + else { + return "Invalid angle"; + } +} + + // The line below allows us to load the getAngleType function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getAngleType; @@ -35,3 +55,18 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); +let reflex = getAngleType(280); +assertEquals(reflex, "Reflex angle"); +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +let invalid = getAngleType(360); +assertEquals(invalid,"Invalid angle"); +invalid = getAngleType(-1); +assertEquals(invalid, "Invalid angle"); +invalid = getAngleType(0); +assertEquals(invalid, "Invalid angle"); +let obtuse = getAngleType(160); +assertEquals(obtuse, "Obtuse angle"); +let acute = getAngleType(10); +assertEquals(acute, "Acute angle"); + 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 970cb9b641..ec8b1d22a6 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,8 +12,14 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (numerator < denominator){ + return true; + } + else{ + return false; + } } - +// console.log(isProperFraction(10,22)) // 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; @@ -31,3 +37,7 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(10,20), true); +assertEquals(isProperFraction(80, 10), false); +assertEquals(isProperFraction(8, 16), true); +assertEquals(isProperFraction(10, 2), 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 ff5c532e1d..ca386020e2 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 @@ -23,6 +23,32 @@ function getCardValue(card) { // TODO: Implement this function + if (typeof card !== "string" || card.length < 2) { + throw new Error("Invalid card format"); + } + const rank = card.slice(0, -1); + const suit = card.slice(-1); + + const validSuits = ["♠", "♥", "♦", "♣"]; + if (!validSuits.includes(suit)) { + throw new Error("Invalid suit"); + } + if (rank === "A") { + return 11; + } + if (["J", "Q", "K"].includes(rank)) { + return 10; + } + const numericValue = parseInt(rank, 10); + if ( + !isNaN(numericValue) && + numericValue >= 2 && + numericValue <= 10 && + String(numericValue) === rank + ) { + return numericValue; + } + throw new Error("Invalid rank"); } // The line below allows us to load the getCardValue function into tests in other files. @@ -52,3 +78,18 @@ try { } // What other invalid card cases can you think of? +assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("2♥"), 2); +assertEquals(getCardValue("10♥"), 10); +assertEquals(getCardValue("A♣"), 11); +assertEquals(getCardValue("J♦"), 10); +assertEquals(getCardValue("Q♠"), 10); +assertEquals(getCardValue("K♣"), 10); + +assertThrows("invalid"); +assertThrows("A"); +assertThrows("♠"); +assertThrows("1♥"); +assertThrows("Z♠"); +assertThrows("10"); +assertThrows("A♥♠"); \ 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 d777f348d3..d5b7beaf6e 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 @@ -13,8 +13,35 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { expect(getAngleType(89)).toEqual("Acute angle"); }); + // Case 2: Right angle +test(`should return "Right angle" when angle is exactly 90`, () => { + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 < angle < 180)`, () => { + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(135)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when angle is exactly 180`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(270)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" for boundaries and out-of-bounds numbers`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(361)).toEqual("Invalid 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 7f087b2ba1..c37bf7569f 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 @@ -8,3 +8,31 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// Case 1: Standard Positive Numbers +test("should handle standard positive fractions correctly", () => { + expect(isProperFraction(1, 2)).toEqual(true); + expect(isProperFraction(5, 3)).toEqual(false); +}); + +// Case 2: Equal Numbers (Boundary) +test("should return false when numerator and denominator are equal", () => { + expect(isProperFraction(5, 5)).toEqual(false); // Equals 1 whole (Improper) +}); + +// Case 3: Numerator is Zero +test("should return true when numerator is zero and denominator is positive", () => { + expect(isProperFraction(0, 5)).toEqual(true); +}); + +// Case 4: Negative Numbers +test("should handle negative integers according to mathematical comparison rules", () => { + expect(isProperFraction(-5, -2)).toEqual(true); + expect(isProperFraction(-1, -4)).toEqual(false); +}); + +// Case 5: Mixed Positive and Negative Numbers +test("should return true when only the numerator is negative", () => { + expect(isProperFraction(-1, 2)).toEqual(true); + expect(isProperFraction(1, -2)).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 cf7f9dae2e..23bd170a0f 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 @@ -9,6 +9,31 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); +// Case 2: Number Cards (2-10) +test("Should return the matching numeric value for number cards (2-10)", () => { + expect(getCardValue("2♥")).toEqual(2); + expect(getCardValue("5♦")).toEqual(5); + expect(getCardValue("10♣")).toEqual(10); +}); + +// Case 3: Face Cards (J, Q, K) +test("Should return 10 when given face cards (J, Q, K)", () => { + expect(getCardValue("J♣")).toEqual(10); + expect(getCardValue("Q♦")).toEqual(10); + expect(getCardValue("K♠")).toEqual(10); +}); + +// Case 4: Invalid Cards + +test("Should throw an error when given invalid card inputs", () => { + expect(() => getCardValue("invalid")).toThrow(); + expect(() => getCardValue("A")).toThrow(); + expect(() => getCardValue("♠")).toThrow(); + expect(() => getCardValue("11♥")).toThrow(); + expect(() => getCardValue("1♥")).toThrow(); + expect(() => getCardValue("Z♦")).toThrow(); +}); + // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) // Face Cards (J, Q, K) diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..ccde6087d8 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (let char of stringOfCharacters) { + if (char === findCharacter) { + count++; + } + } + return count; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf7..360a578f6a 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -17,7 +17,14 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); -// Scenario: No Occurrences +//Scenario: No Occurrences +test("should return 0 when the character does not exist in the string", () => { + const str = "hello"; + const char = "z"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + // Given the input string `str`, // And a character `char` that does not exist within `str`. // When the function is called with these inputs, diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..ac724a2d5b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,21 @@ function getOrdinalNumber(num) { - return "1st"; -} + let lastDigit = num % 10; + let lastTwoDigits = num % 100; + if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) { + return `${num}th`; + } + if (lastDigit === 1) { + return `${num}st`; + } + else if (lastDigit === 2) { + return `${num}nd`; + } + else if (lastDigit === 3) { + return `${num}rd`; + } + else { + 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 adfa58560f..7e118afdd4 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -18,3 +18,12 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); +test("converts 1 to an ordinal number", () => { + expect(getOrdinalNumber(1)).toEqual("1st"); + expect(getOrdinalNumber(11)).toEqual("11th"); +}); +test("converts numbers to ordinal number", () => { + expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(33)).toEqual("33rd"); + expect(getOrdinalNumber(13)).toEqual("13th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..92310e3f6b 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,13 @@ -function repeatStr() { +function repeatStr(str, count) { + let result = ""; + for (let i =0; i { // 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. +// Case: handle count of 1: +test("should return the original string unchanged when count is 1", () => { + const str = "apple"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("apple"); +}); + // 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. +test("should return an empty string when count is 0", () => { + const str = "world"; + const count = 0; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual(""); +}); + // Case: Handle 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, as negative counts are not valid. + +test("should throw an error when count is a negative integer", () => { + const str = "test"; + const count = -5; + expect(() => repeatStr(str, count)).toThrow(); +}); + diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa9..7006ef4032 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -5,9 +5,6 @@ let testName = "Jerry"; const greeting = "hello"; function sayHello(greeting, name) { - const greetingStr = greeting + ", " + name + "!"; - return `${greeting}, ${name}!`; - console.log(greetingStr); } testName = "Aman"; diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index 56d7887c4c..b1c2362d34 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -2,13 +2,8 @@ // The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable. const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); -function logPets(petsArr) { - petsArr.forEach((pet) => console.log(pet)); -} - function countAndCapitalisePets(petsArr) { const petCount = {}; diff --git a/Sprint-3/4-stretch/find.js b/Sprint-3/4-stretch/find.js index c7e79a2f21..24a5f30cf9 100644 --- a/Sprint-3/4-stretch/find.js +++ b/Sprint-3/4-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 +// It always starts at 0, the first letter of the string. On each loop +// iteration that doesn't find a match, index++ runs, increasing its value by 1 + // b) What is the if statement used to check +// If it evaluates to true, it stops the +// entire function immediately and hands back that position number. + // c) Why is index++ being used? +// It forces the loop to move to the next letter in the string for the next round. +// Without it, the loop would look at index 0 over and over again and increasing +// the index in every single turn makes sure that the loop with eventually +// be equal to the length of the string and come to an end. + // d) What is the condition index < str.length used for? +// This condition is a safety guardrail that defines the lifetime of the loop. +// It basically tells the loop to keep looping as long as +// we haven't run out of letters to look at diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..6a2ffffc32 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,5 +1,37 @@ -function passwordValidator(password) { - return password.length < 5 ? false : true +function passwordValidator(password, passwordsHistory) { + if (password.length < 5) { + return false; + } + if (passwordsHistory && passwordsHistory.includes(password)) { + return false; + } + let hasUppercase = false; + let hasLowercase = false; + let hasNumber = false; + let hasSymbol = false; + + const specialSymbols = "!#$%*&."; + for (let i = 0; i < password.length; i++) { + const char = password[i]; + + if (char >= "0" && char <= "9") { + hasNumber = true; + } else if (specialSymbols.includes(char)) { + + hasSymbol = true; + } else if (char === char.toUpperCase() && char !== char.toLowerCase()) { + hasUppercase = true; + + } else if (char === char.toLowerCase() && char !== char.toUpperCase()) { + hasLowercase = true; + } + } + + if (hasUppercase && hasLowercase && hasNumber && hasSymbol) { + return true; + } else { + return false; + } }