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 deleted file mode 100644 index 9e05a871e..000000000 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ /dev/null @@ -1,37 +0,0 @@ -// Implement a function getAngleType -// -// When given an angle in degrees, it should return a string indicating the type of angle: -// - "Acute angle" for angles greater than 0° and less than 90° -// - "Right angle" for exactly 90° -// - "Obtuse angle" for angles greater than 90° and less than 180° -// - "Straight angle" for exactly 180° -// - "Reflex angle" for angles greater than 180° and less than 360° -// - "Invalid angle" for angles outside the valid range. - -// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.) - -// Acceptance criteria: -// After you have implemented the function, write tests to cover all the cases, and -// execute the code to ensure all tests pass. - -function getAngleType(angle) { - // TODO: Implement this function -} - -// 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; - -// This helper function is written to make our assertions easier to read. -// If the actual output matches the target output, the test will pass -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); -} - -// TODO: Write tests to cover all cases, including boundary and invalid cases. -// Example: Identify Right Angles -const right = getAngleType(90); -assertEquals(right, "Right 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 970cb9b64..a5b53ca0c 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,7 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + return Math.abs(numerator) < Math.abs(denominator) } // The line below allows us to load the isProperFraction function into tests in other files. @@ -31,3 +31,27 @@ function assertEquals(actualOutput, targetOutput) { // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); + +//Case 2: 5/5 is not a proper fraction +assertEquals(isProperFraction(5,5), false); + +//Case 3: (-3)/(-5) is a proper fraction +assertEquals(isFinite(-3,-5), true); + +//Case 4: (-4)/6 is a proper fraction +assertEquals(isProperFraction(-4,6),true); + +//case 5: 5/(-8) is a proper fraction +assertEquals(isProperFraction(5,-8), true); + +//case 6: 9/7 is not a proper fraction +assertEquals(isProperFraction(9,7), false); + +//case 7: 7/0 is not a proper fraction +assertEquals(isProperFraction(7,0), false); + +//case 8: 0/6 is a proper fraction +assertEquals(isProperFraction(0,6), true); + +//case 9: 0/-8 is a proper fraction +assertEquals(isProperFraction(0,-8), true); \ 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 c7559e787..98ace579f 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,8 +22,23 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - // TODO: Implement this function -} + const validSuits = ["♠", "♥", "♦", "♣"]; + const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + + if (!validSuits.includes(suit) || !validRanks.includes(rank)) { + throw new Error("Invalid card"); + } + + if (rank === "A") return 11; + if (["J", "Q", "K"].includes(rank)) return 10; + + 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. @@ -38,15 +53,48 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: +// Examples 1 numbers: assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("5♣"), 5); + +//Examples A: +assertEquals(getCardValue("A♦"), 11) + +//Examples J Q K:, +assertEquals(getCardValue("Q♥"), 10) +assertEquals(getCardValue("K♥"), 10) -// Handling invalid cards -try { - getCardValue("invalid"); +//Examples invalid numbers: +assertThrow(()=>getCardValue("1♥")) +assertThrow(()=>getCardValue("11♥")) + +//Examples invalid suit: +assertThrow(()=>getCardValue("A*")) + +//Examples missing rank or suit: +assertThrow(()=>getCardValue("♥")) +assertThrow(()=>getCardValue("5")) + +//Example extra suit: +assertThrow(()=>getCardValue("Q♠♠")) + + +function assertThrow(fn){ +try { (fn) + // we try to run this function, if it throws, stop running this bit and run the catch below // This line will not be reached if an error is thrown as expected console.error("Error was not thrown for invalid card"); -} catch (e) {} +} catch (error) { + // if the above code throws, we catch the error here, that stops the whole program crashing + console.log('there was an error getting card value!',error) +}} + +console.log(' we finished running getCardValue') // What other invalid card cases can you think of? + +//Examples wrong type: +assertThrow(()=>getCardValue("null")) +assertThrow(()=>getCardValue("42")) +cd \ 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 d777f348d..ffbec8e2e 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,24 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => { }); // Case 2: Right angle +test ('should return "Right angle" when angle ===90',() => { + expect(getAngleType(90)).toEqual("Right angle"); +}) // Case 3: Obtuse angles +test ('should return "Obtuse angle" when (90 { + expect(getAngleType(135)).toEqual("Obtuse angle"); +}) // Case 4: Straight angle +test ('should return "Straight angle" when angle ===180',() => { + expect(getAngleType(180)).toEqual("Straight angle"); +}) + // Case 5: Reflex angles +test ('should return "Obtuse angle" when (180 { + expect(getAngleType(275)).toEqual("Reflex angle"); +}) + // Case 6: Invalid angles +test('should return "Invalid angle"when (angle>=360)', ()=> { + expect(getAngleType(360)).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 7f087b2ba..92741097c 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 @@ -4,7 +4,30 @@ 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 +// Special case: denominator is zero test(`should return false when denominator is zero`, () => { expect(isProperFraction(1, 0)).toEqual(false); }); + +//case:absolute numerator is bigger than absolute denominator +test ('should return false when numerator absolute is bigger than denominator absolute',() => { + expect(isProperFraction(5, -4)).toEqual(false); +}) + +//case:numerator is the same as denominator +test('should return false if numerator is the same as denominator', () =>{ + expect(isProperFraction(-9,-9)).toEqual(false) +}) + +//case: absolute numerator is less than absolute denominator +test ('should return true when numerator absolute is less than denominator absolute',() => { + expect(isProperFraction(3,-8)).toEqual(true); +}) + +//case: numerator is zero +test(`should return true when nominator is zero`, () => { + expect(isProperFraction(0,-3)).toEqual(true); +}); + + + 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 cf7f9dae2..5bf037776 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,6 +14,21 @@ test(`Should return 11 when given an ace card`, () => { // Face Cards (J, Q, K) // Invalid Cards +// Case 2 : Number(2-10) +test('should return the exact number when given an number card', () =>{ + expect(getCardValue("5♠")).toEqual(5); +}) + +//case 3 : face card(J,Q,K) +test('should return number when given face card', () =>{ + expect(getCardValue("K♠")).toEqual(10); +}) + +// case 4: Invalid cards +test('throws new Error', () => { + expect(() => getCardValue("9**")).toThrow("Invalid card"); +}); + // 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 diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..2e9b762d7 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 + let count = 0 + for(let i=0;i { + expect(countChar("tired", "d")).toEqual(1) +}) + // Scenario: Multiple Occurrences // Given the input string `str`, @@ -22,3 +26,6 @@ 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 count no occurrences of a character", () => { + expect(countChar("bananataste","w")).toEqual(0) +}) \ 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 f95d71db1..a25d6b24b 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,25 @@ function getOrdinalNumber(num) { - return "1st"; + lastDigit = num % 10; + lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + + switch (lastDigit) { + case 1: + return num + "st"; + case 2: + return num + "nd"; + case 3: + return num + "rd"; + default: + return num + "th"; + } } +console.log(getOrdinalNumber(112)) + + + 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 adfa58560..09469502e 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,31 @@ 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: last two digits ending with 11,12,13 +test("should append 'th' for numbers with last digits 11,12,13", () =>{ + expect(getOrdinalNumber(11)).toEqual("11th"); + expect(getOrdinalNumber(313)).toEqual("313th") + expect(getOrdinalNumber(2112)).toEqual("2112th") +}) + +// Case 3: Numbers ending with 2(but not 12) +test("should append 'nd' for numbers ending with 2, except those ending with 12", () => { + expect(getOrdinalNumber(2)).toEqual("2nd"); + expect(getOrdinalNumber(52)).toEqual("52nd"); + expect(getOrdinalNumber(232)).toEqual("232nd"); +}); + +// Case 4: Numbers ending with 3(but not 13) +test("should append 'rd' for numbers ending with 3, except those ending with 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect(getOrdinalNumber(83)).toEqual("83rd"); + expect(getOrdinalNumber(463)).toEqual("463rd"); +}); + +// case 5: Numbers ending with 4-9 and 0 +test("should append 'th' for numbers with last 4-9 and 0", () =>{ + expect(getOrdinalNumber(8)).toEqual("8th"); + expect(getOrdinalNumber(3130)).toEqual("3130th") + expect(getOrdinalNumber(2119)).toEqual("2119th") +}) diff --git a/Sprint-3/2-practice-tdd/repeat-str.js b/Sprint-3/2-practice-tdd/repeat-str.js index 3838c7b00..13e1a914f 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.js +++ b/Sprint-3/2-practice-tdd/repeat-str.js @@ -1,5 +1,11 @@ -function repeatStr() { - return "hellohellohello"; +function repeatStr(str,count) { + if (count>0) return str.repeat(count) + if (count===0) return "" + if (count<0) return"invalid count" } +console.log(repeatStr("hello",5)) +console.log(repeatStr("nihao",1)) +console.log(repeatStr("nihao",-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 a3fc1196c..d41e4d0e5 100644 --- a/Sprint-3/2-practice-tdd/repeat-str.test.js +++ b/Sprint-3/2-practice-tdd/repeat-str.test.js @@ -20,13 +20,22 @@ 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 repeat the string only once", () => { + expect(repeatStr("nihao",1)).toEqual("nihao"); +}); // 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 repeat the string 0", () => { + expect(repeatStr("newyear",0)).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 repeat the string negative times", () => { + expect(repeatStr("celebration", -5)).toEqual("invalid count"); +}); diff --git a/package.json b/package.json index 0657e22dd..87d27ab8f 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "keywords": [], "author": "Code Your Future", "license": "ISC", - "dependencies": { - "jest": "^29.7.0" + "devDependencies": { + "jest": "^30.2.0" } -} \ No newline at end of file +}