Skip to content
Closed
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
10 changes: 9 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's keep the file tidy without console logs unless it's the aim of the exercise.

Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
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("where","e"))

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)
})
22 changes: 21 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's keep the file tidy without console logs unless it's the aim of the exercise.

Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
function getOrdinalNumber(num) {
return "1st";
lastDigit = num % 10;
lastTwoDigits = num % 100;
Comment on lines +2 to +3
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

While this, in theory, works, without a declaration keyword, they become implicit global variables, which could cause bugs. How can we fix that?


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;
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")
})
12 changes: 9 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's keep the file tidy without console logs unless it's the aim of the exercise.

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str,count) {
if (count>0) return str.repeat(count)
if (count===0) return ""
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What would happen if we were to call str.repeat(0) ? Give it a shot, and once you do, how can we tweak this function slightly to save ourselves from one more if statement?

if (count<0) return"invalid count"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The test is asking for an error to be thrown when the count is negative but we are instead returning a string, how can we address that?

}

module.exports = repeatStr;
console.log(repeatStr("hello",5))
console.log(repeatStr("nihao",1))
console.log(repeatStr("nihao",-5))

module.exports = repeatStr;
9 changes: 9 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,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");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Throwing an error and returning a string is quite different, how can we fix this test?

});
Loading