diff --git a/Project-CLI-Treasure-Hunt b/Project-CLI-Treasure-Hunt new file mode 160000 index 000000000..6668c42dc --- /dev/null +++ b/Project-CLI-Treasure-Hunt @@ -0,0 +1 @@ +Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350 diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2445d911f 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,4 @@ function capitalise(str) { } // =============> write your explanation here -// =============> write your new code here +// =============> write your new code here \ 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 266525d1b..7268ef0fb 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 @@ -8,7 +8,7 @@ // write one test at a time, and make it pass, build your solution up methodically // just make one change at a time -- don't rush -- programmers are deep and careful thinkers function getCardValue(card) { - if (rank === "A") { + if (card === "A♠") { return 11; } } diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..aff5f239a 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,12 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 -} + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + count++; + } + } + return count; +} +console.log(countChar("hello world!", "l")); module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 42baf4b4b..1b29c1edf 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 the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +test("should return 0 when character does not exist in the string", () => { + const str = "hello world"; + const char = "z"; + const count = countChar(str, char); + expect(count).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..41e2cce6c 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.js @@ -1,5 +1,12 @@ function getOrdinalNumber(num) { - return "1st"; + const suffixes = ["th", "st", "nd", "rd"]; + const lastTwoDigits = num % 100; + + if (lastTwoDigits >= 11 && lastTwoDigits <= 13) { + return num + "th"; + } + const suffix = suffixes[(lastTwoDigits % 10)] || "th"; + return num + suffix; } 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 dfe4b6091..a20233ffb 100644 --- a/Sprint-3/2-practice-tdd/get-ordinal-number.test.js +++ b/Sprint-3/2-practice-tdd/get-ordinal-number.test.js @@ -8,6 +8,24 @@ const getOrdinalNumber = require("./get-ordinal-number"); // When the number is 1, // Then the function should return "1st" -test("should return '1st' for 1", () => { +test("append 'st' to numbers ending in 1, except those ending in 11", () => { expect(getOrdinalNumber(1)).toEqual("1st"); + expect( getOrdinalNumber(21) ).toEqual("21st"); + expect( getOrdinalNumber(131) ).toEqual("131st"); }); + +test("append 'nd' to numbers ending in 2, except those ending in 12", () => { + expect( getOrdinalNumber(2) ).toEqual("2nd"); + expect( getOrdinalNumber(22) ).toEqual("22nd"); + expect( getOrdinalNumber(132) ).toEqual("132nd"); +}); +test("append 'rd' to numbers ending in 3, except those ending in 13", () => { + expect(getOrdinalNumber(3)).toEqual("3rd"); + expect( getOrdinalNumber(33) ).toEqual("33rd"); + expect( getOrdinalNumber(133) ).toEqual("133rd"); +}); +test("append 'th' to numbers which are not ending in 1, 2, 3", () => { + expect(getOrdinalNumber(5)).toEqual("5th"); + expect( getOrdinalNumber(24) ).toEqual("24th"); + expect( getOrdinalNumber(138) ).toEqual("138th"); +}); \ No newline at end of file diff --git a/Sprint-3/2-practice-tdd/repeat.js b/Sprint-3/2-practice-tdd/repeat.js index 00e60d7f3..98a7a117c 100644 --- a/Sprint-3/2-practice-tdd/repeat.js +++ b/Sprint-3/2-practice-tdd/repeat.js @@ -1,5 +1,19 @@ -function repeat() { - return "hellohellohello"; +function repeat(str , count) { + if (count < 0) { + throw new Error("Count must be a non-negative integer"); + } + if (count === 0) { + return ""; + } + + let result = ""; + for (let i = 0; i < count; i++) { + result += str; + } + + return result; } + console.log(repeat("hello", 2)); + module.exports = repeat; diff --git a/Sprint-3/2-practice-tdd/repeat.test.js b/Sprint-3/2-practice-tdd/repeat.test.js index 34097b09c..53fe34b0d 100644 --- a/Sprint-3/2-practice-tdd/repeat.test.js +++ b/Sprint-3/2-practice-tdd/repeat.test.js @@ -1,9 +1,5 @@ // Implement a function repeat const repeat = require("./repeat"); -// Given a target string str and a positive integer count, -// When the repeat function is called with these inputs, -// Then it should: - // case: repeat String: // Given a target string str and a positive integer count, // When the repeat function is called with these inputs, @@ -20,13 +16,29 @@ test("should repeat the string count times", () => { // Given a target string str and a count equal to 1, // When the repeat function is called with these inputs, // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. - +test("should return the original string when count is 1", () => { + const str = "world"; + const count = 1; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual("world"); +}); // case: Handle Count of 0: // Given a target string str and a count equal to 0, // When the repeat function is called with these inputs, // Then it should return an empty string, ensuring that a count of 0 results in an empty output. - +test("should return an empty string when count is 0", () => { + const str = "test"; + const count = 0; + const repeatedStr = repeat(str, count); + expect(repeatedStr).toEqual(""); +}); // case: Negative Count: // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. +test("should throw an error for negative count", () => { + const str = "error"; + const count = -2; + + expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); +}); \ No newline at end of file