Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion 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 (char of stringOfCharacters) {
if (char === findCharacter) {
count = count + 1;
}}
return count;
}

module.exports = countChar;
6 changes: 6 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ test("should count multiple occurrences of a character", () => {
const count = countChar(str, char);
expect(count).toEqual(5);
});
test("No Occurrences", () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Functionality-wise, the test is correct. But it is a tiny bit misplaced in the file, and the name could be a bit more descriptive, based on other tests in this file. Can you try to update it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

the peace of code is moved, name is updated

const str = "qwerty";
const char = "k";
const count = countChar(str, char);
expect(count).toEqual(0);
})

// Scenario: No Occurrences
// Given the input string `str`,
Expand Down
16 changes: 15 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,19 @@
function getOrdinalNumber(num) {
return "1st";
let last2digit = num % 100
let lastDigit = num % 10
if (last2digit === 11 || last2digit === 12 || last2digit === 13) {
return `${num}th`
}
else if (lastDigit === 1) {
return `${num}st`
}
else if (lastDigit === 2) {
return `${num}nd`
}
else if (lastDigit === 3) {
return `${num}rd`
}
else 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 @@ -17,4 +17,32 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(111)).toEqual("111th");
});

test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(112)).toEqual("112th");
});

test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

test("should append 'th' for all other numbers", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(14)).toEqual("14th");
expect(getOrdinalNumber(19)).toEqual("19th");
expect(getOrdinalNumber(99)).toEqual("99th");
expect(getOrdinalNumber(1000)).toEqual("1000th");
});

5 changes: 3 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count >= 0) {return str.repeat(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.

One little thing, for readability purposes, it's worth putting return and throw on their own lines with braces.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for mentioning, I have changed the position on the line

else throw new Error("Error")
}

module.exports = repeatStr;
20 changes: 20 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.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.

Functionality-wise, the tests are correct, but it's going to be hard to distinguish which one fails, given that they are all named the same. How can we improve that?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That's true! Didn't think about that. Names's updated:)

Could you please take another look when you have a moment?

Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

test("should repeat the string count times", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// 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 count times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).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 count times", () => {
const str = "hello";
const count = -1;
expect(function(){repeatStr(str, count)}).toThrow()
});