diff --git a/Sprint-3/1-implement-and-rewrite-tests/README.md b/Sprint-3/1-implement-and-rewrite-tests/README.md index a65bd2077c..876ff4cc4c 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/README.md +++ b/Sprint-3/1-implement-and-rewrite-tests/README.md @@ -5,14 +5,14 @@ to choose test values that thoroughly test a function. ## 1 Implement solutions -In the `implement` directory you've got a number of functions you'll need to implement. + +Here is a recommended order: + +1. `1-get-angle-type.js`In the `implement` directory you've got a number of functions you'll need to implement. For each function, you also have a number of different cases you'll need to check for your function. Write your assertions and build up your program case by case. Don't rush to a solution. The point of these assignments is to learn how to write assertions and build up a program step by step. -Here is a recommended order: - -1. `1-get-angle-type.js` 2. `2-is-proper-fraction.js` 3. `3-get-card-value.js` 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..a2ddc0d277 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,30 @@ // execute the code to ensure all tests pass. 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. @@ -33,5 +56,24 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles -const right = getAngleType(90); +let invalid = getAngleType(0); +assertEquals(invalid, "Invalid angle"); +let acute = getAngleType(1); +assertEquals(acute, "Acute angle"); + acute = getAngleType(89); +assertEquals(acute, "Acute angle"); +let right= getAngleType(90); assertEquals(right, "Right angle"); +let obtuse = getAngleType(91); +assertEquals(obtuse, "Obtuse angle"); +obtuse = getAngleType(179); +assertEquals(obtuse, "Obtuse angle"); +const straight = getAngleType(180); +assertEquals(straight, "Straight angle"); +let reflex = getAngleType(181); +assertEquals(reflex, "Reflex angle"); +reflex = getAngleType(359); +assertEquals(reflex, "Reflex angle"); + invalid = getAngleType(400); +assertEquals(invalid, "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..940ed69acb 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 @@ -11,7 +11,18 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if (denominator === 0){ + return false; + } + else if (numerator < denominator) + { + return true; + + } + else + { + return false; + } } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +42,10 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(5, 4), false); +assertEquals(isProperFraction(3, 3), false); +assertEquals(isProperFraction(0, 5), true); +assertEquals(isProperFraction(1, 0), false); +assertEquals(isProperFraction(-3, 2), true); +assertEquals(isProperFraction(2, -5), false); +assertEquals(isProperFraction(-2, -3), 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 c7559e787e..5586b9f134 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 @@ -22,13 +22,37 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function + if(typeof card !== "string" || card.length<2) + { + throw new Error("Invalid card"); + } + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]; + + if(!validSuits.includes(suit) || !validRanks.includes(rank)) + { + throw new Error("Invalid card"); + } + else if (rank == "A") + { + return 11; + } + else if (["J","Q","K"].includes(rank)) +{ + return 10 +} +else { + return Number(rank); +} } // The line below allows us to load the getCardValue function into tests in other files. // This will be useful in the "rewrite tests with jest" step. +//module.exports = getCardValue; module.exports = getCardValue; - // Helper functions to make our assertions easier to read. function assertEquals(actualOutput, targetOutput) { console.assert( @@ -40,13 +64,40 @@ 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("10♥"), 10); // Handling invalid cards try { - getCardValue("invalid"); + getCardValue("11♠"); + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("A?"); +console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("A"); - // This line will not be reached if an error is thrown as expected + + console.error("Error was not thrown for invalid card"); +} catch (e) {} +try { + getCardValue("10"); + + console.error("Error was not thrown for invalid card"); } catch (e) {} // What other invalid card cases can you think of? +//try { + //getCardValue("AA"); +//console.error("Error was not thrown for invalid card"); +//} catch (e) {} +//try { + //getCardValue("1♠"); + + + //console.error("Error was not thrown for invalid card"); +//} catch (e) {} \ 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..efd6ff2c97 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 @@ -9,9 +9,28 @@ const getAngleType = require("../implement/1-get-angle-type"); test(`should return "Acute angle" when (0 < angle < 90)`, () => { // Test various acute angles, including boundary cases expect(getAngleType(1)).toEqual("Acute angle"); - expect(getAngleType(45)).toEqual("Acute angle"); + //expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); +test(`should return "Right angle" when (angle = 90)`, () => { + // Test various acute angles, including boundary cases + expect(getAngleType(90)).toEqual("Right angle"); +}); +test(`should return"Obtuse angle" when (90 < angle< 180)`, ()=>{ + expect(getAngleType(91)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); +test(`should return "Straight angle" when ( angle = 180)`,()=>{ + expect(getAngleType(180)).toEqual("Straight angle"); +}); +test(`should return "Reflex angle" when (180 < angle <360)`, ()=>{ + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); + +}); +test(`should return "invalid angle when angles outside the valid range`, ()=>{ + expect(getAngleType(0)).toEqual("Invalid angle") +}) // Case 2: Right angle // Case 3: Obtuse angles 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..283975aa5b 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,24 @@ const isProperFraction = require("../implement/2-is-proper-fraction"); test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); +test(`Should return true when numerator is zero`, ()=>{ + expect(isProperFraction(0, 5)).toEqual(true); +}); +test(`should return true when denominator is greater than numerator`, () =>{ +expect(isProperFraction(1, 2)).toEqual(true); +}); +test(`should return false when numerator is greater than denominator`, ()=> { + expect(isProperFraction(5, 4)).toEqual(false); +}); +test(`should return false when numerator is equals to denominator`, ()=>{ +expect(isProperFraction(3, 3)).toEqual(false); +}); +test(`should return true when denominator is greater than negative numerator`, ()=>{ + expect(isProperFraction(-3, 2)).toEqual(true); +}); +test(`should return false when numerator is greater than denominator`, ()=> { + expect(isProperFraction(2, -5)).toEqual(false); +}); +test(`should return false when negative numerator is greater than negative denominator`, ()=> { + expect(isProperFraction(-2, -3)).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..d58ba3d71e 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,7 +8,27 @@ const getCardValue = require("../implement/3-get-card-value"); test(`Should return 11 when given an ace card`, () => { expect(getCardValue("A♠")).toEqual(11); }); - +test(`should return 9 when given a 9♠ card`,()=>{ + expect(getCardValue("9♠")).toEqual(9); +}); +test (`should return 10 when given a face card K♦`, ()=>{ + expect(getCardValue("K♦")).toEqual(10); +}); +test(`should return 10 when given a 10`, ()=>{ + expect(getCardValue("10♥")).toEqual(10); +}); +test(`should throw error for invalid Ranks`, ()=>{ + expect(()=> getCardValue("11♠")).toThrow(); +}); +test(`should throw error for invalid Suits`, ()=>{ + expect(()=> getCardValue("A?")).toThrow(); +}); +test(`should throw error for missing Suits`,()=>{ + expect(()=> getCardValue("A")).toThrow(); +}); +test(`should throw error for non String input`, ()=>{ + expect(()=> getCardValue("10")).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/README.md b/Sprint-3/2-practice-tdd/README.md deleted file mode 100644 index f7d82fe43d..0000000000 --- a/Sprint-3/2-practice-tdd/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Practice TDD - -In this section you'll practice this key skill of building up your program test first. - -Use the Jest syntax and complete the provided files, meeting the acceptance criteria for each function. Use the VSCode test runner to run your tests and check your progress. - -Write the tests _before_ the code that will make them pass. - -Recommended order: - -1. `count.test.js` -1. `repeat-str.test.js` -1. `get-ordinal-number.test.js` diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js deleted file mode 100644 index 95b6ebb7d4..0000000000 --- a/Sprint-3/2-practice-tdd/count.js +++ /dev/null @@ -1,5 +0,0 @@ -function countChar(stringOfCharacters, findCharacter) { - return 5 -} - -module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js deleted file mode 100644 index 179ea0ddf7..0000000000 --- a/Sprint-3/2-practice-tdd/count.test.js +++ /dev/null @@ -1,24 +0,0 @@ -// implement a function countChar that counts the number of times a character occurs in a string -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 -// Given the input string `str`, -// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'), -// When the function is called with these inputs, -// Then it should correctly count occurrences of `char`. - -test("should count multiple occurrences of a character", () => { - const str = "aaaaa"; - const char = "a"; - const count = countChar(str, char); - expect(count).toEqual(5); -}); - -// Scenario: No Occurrences -// Given the input string `str`, -// 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. diff --git a/Sprint-3/2-practice-tdd/get-ordinal-number.js b/Sprint-3/2-practice-tdd/get-ordinal-number.js deleted file mode 100644 index f95d71db13..0000000000 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ /dev/null @@ -1,5 +0,0 @@ -function getOrdinalNumber(num) { - return "1st"; -} - -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 deleted file mode 100644 index adfa58560f..0000000000 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ /dev/null @@ -1,20 +0,0 @@ -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. - -// To ensure thorough testing, we need broad scenarios that cover all possible cases. -// Listing individual values, however, can quickly lead to an unmanageable number of test cases. -// Instead of writing tests for individual numbers, consider grouping all possible input values -// into meaningful categories. Then, select representative samples from each category to test. -// This approach improves coverage and makes our tests easier to maintain. - -// Case 1: Numbers ending with 1 (but not 11) -// When the number ends with 1, except those ending with 11, -// 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(131)).toEqual("131st"); -}); diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js deleted file mode 100644 index 3838c7b003..0000000000 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ /dev/null @@ -1,5 +0,0 @@ -function repeatStr() { - return "hellohellohello"; -} - -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 deleted file mode 100644 index a3fc1196c4..0000000000 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ /dev/null @@ -1,32 +0,0 @@ -// Implement a function repeatStr -const repeatStr = require("./repeat-str"); -// Given a target string `str` and a positive integer `count`, -// When the repeatStr function is called with these inputs, -// Then it should: - -// Case: handle multiple repetitions: -// Given a target string `str` and a positive integer `count` greater than 1, -// When the repeatStr function is called with these inputs, -// Then it should return a string that contains the original `str` repeated `count` times. - -test("should repeat the string count times", () => { - const str = "hello"; - const count = 3; - const repeatedStr = repeatStr(str, count); - expect(repeatedStr).toEqual("hellohellohello"); -}); - -// Case: handle count of 1: -// 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 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. - -// 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. diff --git a/Sprint-3/3-dead-code/README.md b/Sprint-3/3-dead-code/README.md deleted file mode 100644 index 2bfbfff819..0000000000 --- a/Sprint-3/3-dead-code/README.md +++ /dev/null @@ -1,9 +0,0 @@ -# Refactoring Dead Code - -Here are two example of code that has not been built efficiently. Both files have dead code in them. It's your job to go back through this existing code, identify the dead code, and remove it so the code is ready for production. - -## Instructions - -1. Work through each `exercise` file inside this directory. -2. Delete the dead code. -3. Commit your changes and make a PR when done. diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js deleted file mode 100644 index 4d09f15fa9..0000000000 --- a/Sprint-3/3-dead-code/exercise-1.js +++ /dev/null @@ -1,17 +0,0 @@ -// Find the instances of unreachable and redundant code - remove them! -// The sayHello function should continue to work for any reasonable input it's given. - -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); - -console.log(greetingMessage); // 'hello, Aman!' diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js deleted file mode 100644 index 56d7887c4c..0000000000 --- a/Sprint-3/3-dead-code/exercise-2.js +++ /dev/null @@ -1,28 +0,0 @@ -// Remove the unused code that does not contribute to the final console log -// 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 = {}; - - petsArr.forEach((pet) => { - const capitalisedPet = pet.toUpperCase(); - if (petCount[capitalisedPet]) { - petCount[capitalisedPet] += 1; - } else { - petCount[capitalisedPet] = 1; - } - }); - return petCount; -} - -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); - -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log diff --git a/Sprint-3/4-stretch/find.js b/Sprint-3/4-stretch/find.js index c7e79a2f21..f554e0e93d 100644 --- a/Sprint-3/4-stretch/find.js +++ b/Sprint-3/4-stretch/find.js @@ -20,6 +20,15 @@ console.log(find("code your future", "z")); // Pay particular attention to the following: // a) How the index variable updates during the call to find +// index starts at 0. +// The while loop runs as long as index is less than the string length. +// Inside the loop, if the character at index matches the target, the function returns that index. +// If it does not match, index increases by 1 and the loop checks again. +// This continues until the character is found or the end of the string is reached. // b) What is the if statement used to check +// The if statement checks the character at the current index, and if it matches the target character, it returns the character index (position in the string). // c) Why is index++ being used? +// index++ increases the index by 1 so the loop can check the next character in the string. // d) What is the condition index < str.length used for? +// index < str.length ensures the loop runs only while the index is within the string length. +// It stops the loop when the end of the string is reached. diff --git a/Sprint-3/4-stretch/password-validator.js b/Sprint-3/4-stretch/password-validator.js index b55d527dba..315d62678e 100644 --- a/Sprint-3/4-stretch/password-validator.js +++ b/Sprint-3/4-stretch/password-validator.js @@ -1,5 +1,35 @@ function passwordValidator(password) { - return password.length < 5 ? false : true + const previousPassword =["Arun!1621"]; + const specialChar = ["!", "#", "$", "%", ".", "*", "&"]; + if (password.length<5) + { + throw new Error("minimum length is 5"); + } + else if(!/[A-Z]/.test(password)) + { + throw new Error("Need at least one uppercase"); + } + else if (!/[a-z]/.test(password)) + { + throw new Error("Need at least one lower case"); + } + else if (!/[0-9]/.test(password)) + { + throw new Error("need at least one number"); + } + else if(!/["!", "#", "$", "%", ".", "*", "&"]/.test(password)) + { + throw new Error("need at least one special character"); + } + else if (previousPassword.includes(password)) + { + throw new Error("no previous password"); + + } +else +{ + return true; +} } diff --git a/Sprint-3/4-stretch/password-validator.test.js b/Sprint-3/4-stretch/password-validator.test.js index 8fa3089d6b..f5b462e229 100644 --- a/Sprint-3/4-stretch/password-validator.test.js +++ b/Sprint-3/4-stretch/password-validator.test.js @@ -17,10 +17,33 @@ You must breakdown this problem in order to solve it. Find one test case first a const isValidPassword = require("./password-validator"); test("password has at least 5 characters", () => { // Arrange - const password = "12345"; - // Act - const result = isValidPassword(password); - // Assert - expect(result).toEqual(true); + const password = "Ha!1"; + expect( ()=> isValidPassword(password)).toThrow(); } -); \ No newline at end of file +); +test("password has at least one uppercase letter(A-Z)",()=>{ +const password = "harju!1621"; + + + expect(()=> isValidPassword(password)).toThrow(); +}); +test("password has at least one lowercase letter (a-z)", ()=>{ + const password = "HARJU!1621"; + expect(()=> isValidPassword(password)).toThrow(); +}); +test("password has at least one number (0-9)", ()=>{ + const password = "Harju!"; + expect(()=>isValidPassword(password)).toThrow(); +}); +test("password has at least one special character ",()=>{ + const password = "Harju1621"; + expect(()=> isValidPassword(password)).toThrow(); +}); +test("password not match with previous password", ()=>{ + const password = "Arun!1621"; + expect(()=> isValidPassword(password)).toThrow(); +}); +test("password meet all the condition", ()=>{ + const password ="Harju!1621"; + expect(isValidPassword(password)).toEqual(true); +}); \ No newline at end of file diff --git a/package.json b/package.json index 0657e22dd8..88a5188578 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "module-structuring-and-testing-data", + "type": "module", "version": "1.0.0", "description": "Like learning a musical instrument, programming requires daily practice.", "main": "index.js",