From 984ec6c0310250c9db4496f235286a62dff3dee3 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 15 Feb 2026 08:36:02 +0000 Subject: [PATCH 1/7] complete code --- .../implement/3-get-card-value.js | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) 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..542ae29fc 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,47 @@ 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")) From 7dac0c8c1c9fff58101c938fb5b2e30694d16a77 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sun, 15 Feb 2026 08:43:28 +0000 Subject: [PATCH 2/7] commit wrong branch --- .../implement/3-get-card-value.js | 40 +------------------ 1 file changed, 1 insertion(+), 39 deletions(-) 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 542ae29fc..6f027cd83 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,21 +22,7 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - 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); } @@ -55,28 +41,6 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. // 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) - -//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){ @@ -87,10 +51,8 @@ try { (fn) console.error("Error was not thrown for invalid card"); } 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? From 168ce7703ce26597ad6f3dd6498cf54cd92a38f5 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 21 Feb 2026 13:57:21 +0000 Subject: [PATCH 3/7] commit --- Sprint-3/2-practice-tdd/count.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 Date: Sat, 21 Feb 2026 14:25:50 +0000 Subject: [PATCH 4/7] complete TDD --- Sprint-3/2-practice-tdd/count.test.js | 9 +++++- Sprint-3/2-practice-tdd/get-ordinal-number.js | 22 ++++++++++++++- .../2-practice-tdd/get-ordinal-number.test.js | 28 +++++++++++++++++++ Sprint-3/2-practice-tdd/repeat-str.js | 12 ++++++-- Sprint-3/2-practice-tdd/repeat-str.test.js | 9 ++++++ 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..c29e334b2 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -2,7 +2,11 @@ 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: +// Then it should: +test("should count occurrences of a character", () => { + 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..c7e8954ea 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") +}) \ 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 3838c7b00..fbcb3eb55 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" } -module.exports = repeatStr; +console.log(repeatStr("hello",5)) +console.log(repeatStr("nihao",1)) +console.log(repeatStr("nihao",-5)) + +module.exports = repeatStr; \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat-str.test.js b/Sprint-3/2-practice-tdd/repeat-str.test.js index a3fc1196c..58087b0c6 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"); +}); \ No newline at end of file From 8acbbf2bd061bc78123be0b5d53a6d813d23b42b Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 21 Feb 2026 15:01:50 +0000 Subject: [PATCH 5/7] removed file --- .../implement/3-get-card-value.js | 61 ------------------- 1 file changed, 61 deletions(-) delete mode 100644 Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js 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 deleted file mode 100644 index 6f027cd83..000000000 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ /dev/null @@ -1,61 +0,0 @@ -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck - -// Implement a function getCardValue, when given a string representing a playing card, -// should return the numerical value of the card. - -// A valid card string will contain a rank followed by the suit. -// The rank can be one of the following strings: -// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" -// The suit can be one of the following emojis: -// "♠", "♥", "♦", "♣" -// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". - -// When the card is an ace ("A"), the function should return 11. -// When the card is a face card ("J", "Q", "K"), the function should return 10. -// When the card is a number card ("2" to "10"), the function should return its numeric value. - -// When the card string is invalid (not following the above format), the function should -// throw an error. - -// 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 getCardValue(card) { - -} - - -// 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; - -// Helper functions to make our assertions easier to read. -function assertEquals(actualOutput, targetOutput) { - console.assert( - actualOutput === targetOutput, - `Expected ${actualOutput} to equal ${targetOutput}` - ); -} - -// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples 1 numbers: -assertEquals(getCardValue("9♠"), 9); - - -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 (error) { - // if the above code throws, we catch the error here, that stops the whole program crashing - - - -// What other invalid card cases can you think of? - -//Examples wrong type: -assertThrow(()=>getCardValue("null")) -assertThrow(()=>getCardValue("42")) From 649d8040faa33123e5ad36f159c061e88efd49d2 Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 21 Feb 2026 15:05:47 +0000 Subject: [PATCH 6/7] restore file to match main --- .../implement/3-get-card-value.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js 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 new file mode 100644 index 000000000..6f027cd83 --- /dev/null +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -0,0 +1,61 @@ +// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck + +// Implement a function getCardValue, when given a string representing a playing card, +// should return the numerical value of the card. + +// A valid card string will contain a rank followed by the suit. +// The rank can be one of the following strings: +// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" +// The suit can be one of the following emojis: +// "♠", "♥", "♦", "♣" +// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦". + +// When the card is an ace ("A"), the function should return 11. +// When the card is a face card ("J", "Q", "K"), the function should return 10. +// When the card is a number card ("2" to "10"), the function should return its numeric value. + +// When the card string is invalid (not following the above format), the function should +// throw an error. + +// 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 getCardValue(card) { + +} + + +// 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; + +// Helper functions to make our assertions easier to read. +function assertEquals(actualOutput, targetOutput) { + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` + ); +} + +// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. +// Examples 1 numbers: +assertEquals(getCardValue("9♠"), 9); + + +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 (error) { + // if the above code throws, we catch the error here, that stops the whole program crashing + + + +// What other invalid card cases can you think of? + +//Examples wrong type: +assertThrow(()=>getCardValue("null")) +assertThrow(()=>getCardValue("42")) From 9a2b25737eb69cf086620f3b2de66340f0c587ec Mon Sep 17 00:00:00 2001 From: pathywang Date: Sat, 21 Feb 2026 15:07:44 +0000 Subject: [PATCH 7/7] restored file --- .../implement/3-get-card-value.js | 23 ++++++------------- 1 file changed, 7 insertions(+), 16 deletions(-) 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 6f027cd83..c7559e787 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,9 +22,8 @@ // execute the code to ensure all tests pass. function getCardValue(card) { - -} - + // TODO: Implement this function +} // 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. @@ -39,23 +38,15 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples 1 numbers: +// Examples: assertEquals(getCardValue("9♠"), 9); - -function assertThrow(fn){ -try { (fn) - // we try to run this function, if it throws, stop running this bit and run the catch below +// Handling invalid cards +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"); -} catch (error) { - // if the above code throws, we catch the error here, that stops the whole program crashing - - +} catch (e) {} // What other invalid card cases can you think of? - -//Examples wrong type: -assertThrow(()=>getCardValue("null")) -assertThrow(()=>getCardValue("42"))