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..48ef0537b0 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 @@ -15,7 +15,32 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + // TODO: Implement this function + // first converting conditions into variables + const acuteAngle = (angle > 0 && angle < 90); + const rightAngle = (angle === 90); + const obtuseAngle = (angle > 90 && angle < 180); + const straightAngle = (angle === 180); + const reflexAngle = (angle > 180 && angle < 360); + const zeroAngle = (angle === 0); + + + if (rightAngle) { + // checking for conditions + return "Right angle"; + } else if (zeroAngle) { + return "Invalid angle"; + }else if (obtuseAngle) { + return "Obtuse angle"; + } else if (straightAngle) { + return "Straight angle"; + } else if (reflexAngle) { + return "Reflex angle"; + } else if (acuteAngle) { + return "Acute angle"; + } else if (angle === zeroAngle || angle != acuteAngle || angle != rightAngle || angle != obtuseAngle || angle != straightAngle || angle != reflexAngle) { + return "Invalid angle"; + } } // The line below allows us to load the getAngleType function into tests in other files. @@ -35,3 +60,24 @@ 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 invalidAngle1 = getAngleType(360); +assertEquals(invalidAngle1, "Invalid angle"); + +const invalidAngle2 = getAngleType(0); +assertEquals(invalidAngle2, "Invalid angle"); + +const invalidAngle3 = getAngleType(-1); +assertEquals(invalidAngle3, "Invalid 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..4036ee5293 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 (Math.abs(numerator / denominator) < 1) { + return true;// if numerator is smaller than denominator - return true + } else { + return false;//return false if numerator if bigger that denominator + } } + // 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,12 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +// Example: 2/3 is a proper fraction +assertEquals(isProperFraction(2, 3), true); + +// Example: 4/2 is not a proper fraction +assertEquals(isProperFraction(4, 2), false); + +// Example: 4/4 is not a proper fraction +assertEquals(isProperFraction(4, 4), false); 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..20302100a2 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,41 @@ function getCardValue(card) { // TODO: Implement this function + const suit = ["♠", "♥", "♦", "♣"]; // array with suits + const rank = [ + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "A", + "K", + "Q", + "J", + ]; // array with ranks + const tenPointsFaceCards = ["K", "Q", "J"]; //cards with value of 10 points + + const rank1 = card.slice(0, -1); //takes the firs part of card + const suit1 = card.slice(-1); //takes "suit" secnd part of card + const isCardValid = suit.includes(suit1) && rank.includes(rank1); //conditions where card is considered valid + + //conditional statement to check validity of the card + if (!isCardValid) { + throw new Error("Invalid card format"); + } + + //conditional statements to check what value to return depending on the card value + if (rank1 === "A") { + return 11; + } else if (tenPointsFaceCards.includes(rank1)) { + return 10; + } else { + return Number(rank1); + } } // The line below allows us to load the getCardValue function into tests in other files. @@ -39,8 +74,23 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // Examples: + assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("A♠"), 11); + +assertEquals(getCardValue("K♠"), 10); + +assertEquals(getCardValue("Q♠"), 10); + +assertEquals(getCardValue("J♠"), 10); + +assertEquals(getCardValue("10♣"), 10); + +//assertEquals(getCardValue("11♣"), "Invalid card format"); i find that +// this test will not work, because my function is throwing error before the program +// reaches the assertEquals function. + // Handling invalid cards try { getCardValue("invalid"); @@ -52,3 +102,83 @@ try { } // What other invalid card cases can you think of? +try { + getCardValue("11♣"); // testing with "11♣" the error will be thrown because "11" is not a valid rank + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("A"); // testing with "A" the error will be thrown because there is no suit + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("10"); // testing with "10" the error will be thrown because there is no suit + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("1♣"); // testing with "1♣" the error will be thrown because "1" is not a valid rank + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("A5"); // testing with "A5" the error will be thrown because "5" is not a valid suit + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("♣6"); // testing with "♣6" the error will be thrown because "6" is not a valid suit and "♣" is not a valid rank + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("@@"); // testing with "@@" the error will be thrown because "@" is not a valid rank or suit + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue("-6♣"); // testing with "-6♣" (negative number) the error will be thrown because "-" is not a valid rank + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} + +try { + getCardValue(""); // testing with "" the error will be thrown because "" is not a valid card + + // This line will not be reached if an error is thrown as expected + console.error("Error was not thrown for invalid card 😢"); +} catch (e) { + console.log("Error thrown for invalid card 🎉"); +} 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..10f83ecf40 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 @@ -14,7 +14,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test(`should return "Right angle" when (angle === 90)`, () => { + //test right angle at 90 degrees + expect(getAngleType(90)).toEqual("Right angle"); +}); + // Case 3: Obtuse angles +test(`should return "Obtuse angle" when (90 > angle > 180)`, () => { + //test various obtuse cases, including border cases + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(120)).toEqual("Obtuse angle"); + expect(getAngleType(179.9)).toEqual("Obtuse angle"); +}); // Case 4: Straight angle +test(`should return "Straight angle" when (angle ==== 180)`, () => { + //test for straight angle at 180 degrees + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 > angle > 360)`, () => { + //test for various reflex angle between 181 and 359 degrees + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(210)).toEqual("Reflex angle"); + expect(getAngleType(359.9)).toEqual("Reflex angle"); +}); // Case 6: Invalid angles +test(`should return "Invalid angle" when (angle >= 0 || angle >= 360 || angle === isNan())`, () => { + //tests for various invalid angles + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(-10)).toEqual("Invalid angle"); + expect(getAngleType(380)).toEqual("Invalid angle"); + expect(getAngleType(NaN)).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..5dc18ea3e3 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 @@ -5,6 +5,63 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); // TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories. // Special case: numerator is zero +test(`should return true when numerator is zero `, () => { + expect(isProperFraction(0, 2)).toEqual(true); +}); + +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + + + +// numerator and denominator are both zero +test(`should return false when numerator and denominator are both zero`, () => { + expect(isProperFraction(0, 0)).toEqual(false); +}); + +// negative numerator and positive denominator +test(`should return true when numerator is negative and denominator is positive`, () => { + expect(isProperFraction(-1, 2)).toEqual(true); +}); + +// positive numerator, negative denominator +test(` should return false when numerator is positive and denominator is negative`, () => { + expect(isProperFraction(1, -2)).toEqual(true); +}) + +//negative numerator and denominator where numerator is smaller than denominator +test(`should return true when numerator is negative and denominator is negative and numerator is smaller than denominator`, () => { + expect(isProperFraction(-1, -2)).toEqual(true); +}); + +//negative numerator and denominator where numerator is bigger than denominator +test(`should return false when numerator is negative and denominator is negative and numerator is bigger than denominator`, () => { + expect(isProperFraction(-2, -1)).toEqual(false); +}); + +//positive numerator and denominator where numerator is smaller +test(`should return true when numerator is smaller`, () => { + expect(isProperFraction(1, 2)).toEqual(true); +}); + +// positive numerator and denominator, numerator is larger +test(`should return false when numerator is larger`, () => { + expect(isProperFraction(4, 2)).toEqual(false); +}); + +// equal numerator and denominator +test(`should return false when numerator and denominator are equal`, () => { + expect(isProperFraction(4, 4)).toEqual(false); +}); + +// numerator and denominator are both floating point numbers, numerator is smaller +test(`should return true when numerator and denominator are both floating point numbers and numerator is smaller`, () => { + expect(isProperFraction(1.5, 2.5)).toEqual(true); +}); + +// numerator and denominator are both floating point numbers, numerator is larger +test(`should return false when numerator and denominator are both floating point numbers and numerator is larger`, () => { + expect(isProperFraction(2.5, 1.5)).toEqual(false); +}); 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..5f43a43778 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 @@ -14,7 +14,49 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +// Case 2: Number cards (2-9) // To learn how to test whether a function throws an error as expected in Jest, // please refer to the Jest documentation: // https://jestjs.io/docs/expect#tothrowerror +test(`Should return corresponding number when given a number card`, () => { + 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); +}); + +// Case 3: Number cards (10) +test(`Should return 10 when given a 10 card`, () => { + expect(getCardValue("10♠")).toEqual(10); +}); + +// Case 4: Face cards (J, Q, K) +test(`Should return 10 when given a J card`, () => { + expect(getCardValue("J♠")).toEqual(10); +}); + +// Case 5: Face cards (J, Q, K) +test(`Should return 10 when given a Q card`, () => { + expect(getCardValue("Q♠")).toEqual(10); +}); + +// Case 6: Face cards (J, Q, K) +test(`Should return 10 when given a K card`, () => { + expect(getCardValue("K♠")).toEqual(10); +}); + +// Invalid cases +test(`should throw an error with message "Invalid card format" when given invalid card`, () => { + expect(() => getCardValue("1♠")).toThrow("Invalid card format"); + expect(() => getCardValue("11♥")).toThrow("Invalid card format"); + expect(() => getCardValue("A1")).toThrow("Invalid card format"); + expect(() => getCardValue("1A")).toThrow("Invalid card format"); + expect(() => getCardValue("♣10")).toThrow("Invalid card format"); + expect(() => getCardValue("$$")).toThrow("Invalid card format"); + expect(() => getCardValue("-5♣")).toThrow("Invalid card format"); +}); diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d4..9e0239a4a0 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} +// 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..4bb07d346d 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,10 @@ 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 detect no instances of a given character", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}) + diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js index f95d71db13..3a38b9fc01 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,34 @@ function getOrdinalNumber(num) { - return "1st"; + //return "1st"; + if (num % 100 === 11) { + return `${num}th`; + } else if (num % 100 === 12) { + return `${num}th`; + } else if (num % 100 === 13) { + return `${num}th`; + } else if (num % 10 === 1) { + return `${num}st`; + } else if (num % 10 === 2) { + return `${num}nd` + } else if (num % 10 === 3) { + return `${num}rd` + } else { + return `${num}th` + } } module.exports = getOrdinalNumber; + +console.log(getOrdinalNumber(1)); +console.log(getOrdinalNumber(2)); +console.log(getOrdinalNumber(3)); +console.log(getOrdinalNumber(4)); +console.log(getOrdinalNumber(11)); +console.log(getOrdinalNumber(12)); +console.log(getOrdinalNumber(13)); +console.log(getOrdinalNumber(21)); +console.log(getOrdinalNumber(52)); +console.log(getOrdinalNumber(63)); +console.log(getOrdinalNumber(111)) + + 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..b426707ff5 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,39 @@ test("should append 'st' for numbers ending with 1, except those ending with 11" expect(getOrdinalNumber(21)).toEqual("21st"); expect(getOrdinalNumber(131)).toEqual("131st"); }); + +//Case 2: Numbers ending with 2 (but not 12) +// When the number ends with 2, except those ending with 12, +// Then the function should return a string by appending "nd" to the number. +test(`should return 'nd' for numbers ending with 2`, () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(22)).toEqual("22nd"); + expect(getOrdinalNumber(122)).toEqual("122nd"); +}) + +//Case 3: Numbers ending with 3 (but not 13) +// When the number ends with 3, except those ending with 13, +// Then the function should return a string by appending "rd" to the number. +test(`should add 'rd' for numbers ending with 3`, () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(23)).toEqual("23rd"); + expect(getOrdinalNumber(123)).toEqual("123rd"); +}) + +//Case 4: Numbers ending with 11, 12, 13. +// When the number ends with 11, 12 and 13, +// Then the function should return a string by appending "th" to the number. + test(`should add 'th' to exceptions like 11, 12 and 13`, () => { + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(12)).toEqual("12th"); + expect(getOrdinalNumber(13)).toEqual("13th"); + }) + + //Case 5: All other numbers that are not ending with 1, 2, 3, and that are not 11, 12, and 13. +// the function should return corresponding string with "th" at the end +test(`should add 'th' to every case that is not exceptional`, () => { + expect(getOrdinalNumber(4)).toEqual("4th"); + expect(getOrdinalNumber(555)).toEqual("555th"); + expect(getOrdinalNumber(6666)).toEqual("6666th"); +}) + diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 2af0a2cea7..fb102627cb 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,7 +1,23 @@ -function repeatStr() { +function repeatStr(str, num) { // Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat). // The goal is to re-implement that function, not to use it. - return "hellohellohello"; + //return "hellohellohello"; + var result = ""; + var i; + if (num < 0) { + throw new Error("negative numbers are not valid"); + } else if (num === 0) { + result = ""; + } else { + for (i = 0; i < num; i++) { + result += str; + } + } + return result; } +//console.log(repeatStr("hello", -1)); +//console.log(repeatStr("hello", 0)); +//console.log(repeatStr("hello", 5)) + module.exports = repeatStr; diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c4..858e40e1be 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,31 @@ test("should repeat the string count times", () => { // 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. +test("should return just original string", () => { + const str = "hello"; + const count = 1; + const repeatedStr = repeatStr(str, count); + expect(repeatedStr).toEqual("hello"); +}); // 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 for count of 0", () => { + const str = "hello"; + 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 negative number is entered", () => { + const str = "hello"; + const count = -1; + + expect(() => repeatStr(str, count)).toThrow("negative numbers are not valid"); +}); diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa9..484e6561af 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -5,12 +5,9 @@ let testName = "Jerry"; const greeting = "hello"; function sayHello(greeting, name) { - const greetingStr = greeting + ", " + name + "!"; return `${greeting}, ${name}!`; - console.log(greetingStr); } -testName = "Aman"; const greetingMessage = sayHello(greeting, testName); 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 = {};