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..c3a4517b9d 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 @@ -1,3 +1,4 @@ + // Implement a function getAngleType // // When given an angle in degrees, it should return a string indicating the type of angle: @@ -16,6 +17,19 @@ 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. @@ -35,3 +49,21 @@ function assertEquals(actualOutput, targetOutput) { // Example: Identify Right Angles const right = getAngleType(90); assertEquals(right, "Right angle"); + +const acute = getAngleType(45); +assertEquals(acute, 'Acute angle'); + +const obtuse = getAngleType(120); +assertEquals(obtuse,'obtuse angle') + +const straight = getAngleType(180); +assertEquals(straight, 'Straight angle'); + +const reflex = getAngleType(270); +assertEquals(reflex, 'Reflex angle'); + +const invalid1 = getAngleType(-10); +assertEquals(invalid1, 'Invalid angle'); + +const invalid2 = getAngleType(400); +assertEquals(invalid2, 'Invalid 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 970cb9b641..059e00b036 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,6 +12,14 @@ function isProperFraction(numerator, denominator) { // TODO: Implement this function + if (denominator === 0){ + return fasle; + + } + if (numerator < denominator && numerator > 0){ + return true; + } + return false; } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +39,15 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// exapmle : 4/3 is not a proper fraction +assertEquals(isProperFraction(4, 3), false); + +// example: 6/5 is not a proper fraction +assertEquals(isProperFraction(6, 5), false); + +// example: 0/5 is a proper fraction +assertEquals(isProperFraction(0, 5), true); + +// example: 5/0 is not a proper fraction +assertEquals(isProperFraction(5, 0), 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..a58657833b 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,21 @@ function getCardValue(card) { // TODO: Implement this function + if(card === "A♠" || card === "A♥" || card === "A♦" || card === "A♣"){ + return 11; + } + else if(card === "J♠" || card === "J♥" || card === "J♦" || card === "J♣" || card === "Q♠" || card === "Q♥" || card === "Q♦" || card === "Q♣" || card === "K♠" || card === "K♥" || card === "K♦" || card === "K♣"){ + return 10; + } + else if(card === "2♠" || card === "2♥" || card === "2♦" || card === "2♣"){ + return 2; + } + else if(card === "3♠" || card === "3♥" || card === "3♦" || card === "3♣"){ + return 3; + } + else { + throw new Error("Invalid card"); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -46,9 +61,34 @@ try { getCardValue("invalid"); // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card 😢"); + console.error("Error was not thrown for invalid card "); } catch (e) { - console.log("Error thrown for invalid card 🎉"); + console.log("Error thrown for invalid card "); } +assertEquals(getCardValue("A♠"), 11); +assertEquals(getCardValue("J♣"), 10); +assertEquals(getCardValue("Q♦"), 10); +assertEquals(getCardValue("K♥"), 10); +assertEquals(getCardValue("2♠"), 2); +assertEquals(getCardValue("3♥"), 3); + +// What other valid card cases can you think of? +function assertEqual(func, valid message){ + try{ + func(); + console.log("Valid card test passed "); + } // What other invalid card cases can you think of? + function assertThrows(func, errorMessage) { + try { + func(); + console.error("Error was not thrown "); + } catch (e) { + if (e.message === errorMessage) { + console.log("Error thrown as expected "); + } else { + console.error(`Unexpected error message: ${e.message}`); + } + } +} \ 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..33af2c967a 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 @@ -6,7 +6,7 @@ const getAngleType = require("../implement/1-get-angle-type"); // including boundary and invalid cases. // Case 1: Acute angles -test(`should return "Acute angle" when (0 < angle < 90)`, () => { +test(`should return "Acute angle" when (0 < angle && angle < 90)`, () => { // Test various acute angles, including boundary cases expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); @@ -14,7 +14,28 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test (`should return "right angle" when (angle === 90)`,() => { + // testing various angles ,including boundary cases + expect( getAngleType(90)).toEqual("right angle"); + }); // Case 3: Obtuse angles +test (`should return "obtuse angle" when (90 < angle && angle < 180)`, () => { +expect(getAngleType(91)).toEqual("obtuse angle"); +expect(getAngleType(125)).toEqual("obtuse angle"); +expect(getAngleType(172)).toEqual("obtuse angle"); +}); // Case 4: Straight angle -// Case 5: Reflex angles +test(`should return "straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("straight angle"); +}); +// Case 5: Reflex angles\ +test(`should return "reflex angle" when (180 < angle && 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" when (angle < 0 || angle > 360)`, () => { + expect(getAngleType(-1)).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..69cda3bf52 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,17 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +// special case : numerator is one +test(` should return true when the numerator is one and denominator is greater than one`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); +// special case : denominator is negative to the numerator +test(`should return false when the denominator is negative and the numerator is positive`, () => { + expect(isProperFraction(1, -2)).toEqual(false); +}); + +// special case : denominator is negative to the numerator +test(`should return false when the numerator is negative and the denominator is positive`, () => { + 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..3786982ab6 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,10 +9,24 @@ test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); -// Suggestion: Group the remaining test data into these categories: -// Number Cards (2-10) -// Face Cards (J, Q, K) -// Invalid Cards +// case 2 : face cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + expect(getCardValue("J♠")).toEqual(10); + expect(getCardValue("Q♥")).toEqual(10); + expect(getCardValue("K♦")).toEqual(10); +}); + +// Case 3: Number Cards (2-10) +test(`Should return the correct value when given a number card`, () => { + expect(getCardValue("2♠")).toEqual(2); + expect(getCardValue("5♥")).toEqual(5); + expect(getCardValue("10♦")).toEqual(10); +}); + +// Case 4: Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + expect(() => getCardValue("invalid")).toThrow("Invalid card"); +}); // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..719fddbefa 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -3,3 +3,9 @@ function countChar(stringOfCharacters, findCharacter) { } module.exports = countChar; + test( "should count multiple occurrences of a character ", function(){ + const stringofcharacters = "aaaaa"; + const findcharcaters = "a"; + const count = countChar(stringofcharacters, findcharcaters); + expect(count).toEqual(5); + }); \ 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 179ea0ddf7..7c494f3337 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -3,6 +3,15 @@ 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: +function CountChar(str, char) { + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === char) { + count++; + } + } + return count; +} // Scenario: Multiple Occurrences // Given the input string `str`, @@ -22,3 +31,9 @@ test("should count multiple occurrences of a character", () => { // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. +test('should return 0 when the character does not occur in the string', () => { + const str = "hello"; + const char = "x"; + const count = countChar(str, char); + expect(count).toEqual(5); +}); \ 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 f95d71db13..e56ce47b94 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -3,3 +3,13 @@ function getOrdinalNumber(num) { } module.exports = getOrdinalNumber; + test(`works with any number ending with 1, except for 11. For all other numbers, it should return the number followed by "th" with exceptions to 2 and 3 `, () => { + expect(getOrdinalNumber(1)).toBe("1st"); + expect(getOrdinalNumber(2)).toBe("2nd"); + expect(getOrdinalNumber(3)).toBe("3rd"); + expect(getOrdinalNumber(4)).toBe("4th"); + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(12)).toBe("12th"); + }); + + \ 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 adfa58560f..1df44e4458 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -15,6 +15,18 @@ const getOrdinalNumber = require("./get-ordinal-number"); // Then the function should return a string by appending "st" to the number. test("should append 'st' for numbers ending with 1, except those ending with 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); - expect(getOrdinalNumber(21)).toEqual("21st"); + expect(getOrdinalNumber(31)).toEqual("31st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + test(`should append 'nd' for numbers ending with 2 except those ending with 12 `, () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(132)).toEqual("132nd"); +}); + + test(`should append 'rd' for numbers ending with 3 except those ending with 13 `, () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(143)).toEqual("143rd"); +}); + \ No newline at end of file diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa9..a68f274f26 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -5,9 +5,8 @@ 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..d865830a2e 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -2,12 +2,10 @@ // 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..6ab0d27c9b 100644 --- a/Sprint-3/4-stretch/find.js +++ b/Sprint-3/4-stretch/find.js @@ -20,6 +20,24 @@ 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 index variable starts at 0 and is incremented by 1 after each iteration of the while loop. + // b) What is the if statement used to check +// the if statement is used to check if the character at the current index of the string is equal to the character we are searching for. +// if the condition is true ,it will return the index of the character in the string. ' +// if the condition is false, it will continue to the next iteration of the loop until it finds the character or reaches the end of the string. + // c) Why is index++ being used? +/* + the index++ is used to increment the index variable by 1 after each iteration of the loop. +This allows the loop to move to the next character in the string for the next iteration. Without this increment, +the loop would get stuck on the same character and could potentially run indefinitely if the condition is never met. +*/ + // d) What is the condition index < str.length used for? +/* +the index < str.length condition is used to ensure that the loop continues to iterate through the string until it reaches the end. +the index variable is used to keep track of the current position in the string, and the loop will continue as long as index is less than the length of the string. +Once index reaches the length of the string, + it means we have checked all characters in the string, and if we haven't found the character we're looking for, we return -1 to indicate that it was not found. + */ \ No newline at end of file diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..615e3d0b8c 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -3,4 +3,13 @@ function passwordValidator(password) { } +module.exports = passwordValidator; + function passwordValidator(password) { + if (password.length < 5){ + return false; + } else if (password.length >= 5 ){ + return true; + } +} + module.exports = passwordValidator; \ No newline at end of file diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6b..eb230a67a1 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -23,4 +23,47 @@ test("password has at least 5 characters", () => { // Assert expect(result).toEqual(true); } -); \ No newline at end of file +); +// instruction 2 +const isValidPassword = require("./password-validator"); +test("password has least one uppercase letter", () => { + // arrange + const uppercase = "A -Z"; + // act + const result = isValidPassword(uppercase); + // assert + expect(result).toEqual(true); +}); + +// instruction 3 +const isValidPassword = require("./password-validator"); +test("password has least one lowercase letter", () => { + // arrange + const lowercase = "a-z"; + // act + const result = isValidPassword(lowercase); + // assert + expect(result).toEqual(true); +}); + +// instruction 4 +const isValidPassword = require("./password-validator"); +test("password has least one number", () => { + // arrange + const number = "0-9"; + // act + const result = isValidPassword(number); + // assert + expect(result).toEqual(true); +}); + +// instruction 5 +const isValidPassword = require("./password-validator"); +test("password has least one non-alphanumeric symbol", () => { + // arrange + const symbol = "!#$%.*&"; + // act + const result = isValidPassword(symbol); + // assert + expect(result).toEqual(true); +}); \ No newline at end of file