Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
11 changes: 10 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

for (let character of stringOfCharacters) {
if (character === findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;


20 changes: 20 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,28 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character", () => {
const str = "banana";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(3);
});

test("should count multiple occurrences of a character", () => {
const str = "ananas";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(3);
});

Comment on lines +20 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

How are these two tests different from the test on lines 13-18? They are all testing "should count multiple occurrences of a character". If they belong to the same category, we could test them in the following manner:

    expect( countChar("aaaaa", "a") ).toEqual(5);
    expect( countChar("ananas", "a") ).toEqual(3);
    ...

Can you think of different cases which we could test?

Copy link
Author

Choose a reason for hiding this comment

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

yes, i forget the last criteria when there is no char in the string and the output is 0.

// Scenario: No Occurrences
// Given the input string str,
// 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 count no occurrence of a character", () => {
const str = "hello";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
9 changes: 8 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,12 @@
function getOrdinalNumber(num) {
return "1st";
const last = num % 10;
const lastTwo = num % 100;

if (last === 1 && lastTwo !== 11) return num + "st";
if (last === 2 && lastTwo !== 12) return num + "nd";
if (last === 3 && lastTwo !== 13) return num + "rd";
return num + "th";
}

module.exports = getOrdinalNumber;

8 changes: 5 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
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(221) ).toEqual("221st");
});

12 changes: 10 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function repeat() {
return "hellohellohello";
function repeat(word, n) {
if (typeof n !== 'number' || n < 0) {
throw new Error("Count must be a non-negative integer");
}
if (n === 0) {
return "";
}
return word.repeat(n);
}

module.exports = repeat;


18 changes: 18 additions & 0 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,30 @@ test("should repeat the string count times", () => {
// 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 repeat the string count times", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hello");
});
// 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 repeat the string count times", () => {
const str = "hello";
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 repeat the string count times", () => {
const str = "hello";
const count = -1;
expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer");
});