Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
let count = 0
for(let i=0;i<stringOfCharacters.length;i++ ){
if (stringOfCharacters[i]===findCharacter){
count++
}
}
return count}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move the closed bracket to a new line for readability, but it looks great otherwise!


module.exports = countChar;
9 changes: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand All @@ -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)
})
17 changes: 16 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function getOrdinalNumber(num) {
return "1st";
let lastDigit = num % 10;
let 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";
}
}

module.exports = getOrdinalNumber;
28 changes: 28 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
8 changes: 6 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count cannot be negative");
}

return str.repeat(count);
}

module.exports = repeatStr;
10 changes: 10 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ 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 throw an error when count is negative", () => {
expect(() => repeatStr("celebration", -5))
.toThrow("Count cannot be negative");
});