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: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

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

module.exports = countChar;
15 changes: 15 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const countChar = require("./count");
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').


test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
Expand All @@ -22,3 +23,17 @@ 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);
});


// Add case sensitivity tests
test("should be case-sensitive", () => {
expect(countChar("AaAa", "A")).toEqual(2);
expect(countChar("AaAa", "a")).toEqual(2);
});
15 changes: 14 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,18 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwo = num % 100;
const last = num % 10;

// Special exceptions: 11, 12, 13
if (lastTwo === 11 || lastTwo === 12 || lastTwo === 13) {
return `${num}th`;
}

// Regular cases
if (last === 1) return `${num}st`;
if (last === 2) return `${num}nd`;
if (last === 3) return `${num}rd`;

return `${num}th`;
}

module.exports = getOrdinalNumber;
27 changes: 26 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Test categories are comprehensives.
Could test more samples in each category.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ 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(101)).toEqual("101st");
});

//identify ordinal for nd
test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

//identify ordinal for rd
test("append 'rd' to numbers ending in 3, except those ending in 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
});

//Special English Rule... 12, 13, 14 ending in "th"
test("append 'th' to numbers that end in 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

//All other numbers => th
test("append 'th' to all other numbers", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

"numbers ending in 0, 4-9" is probably clearer than "all other numbers".

Copy link
Author

Choose a reason for hiding this comment

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

great stuffs

expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
13 changes: 11 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {
if (count < 0) {
throw new Error("Count must be non-negative");
}

let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;
}

module.exports = repeat;

41 changes: 19 additions & 22 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
// 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,
// Then it should repeat the str count times and return a new string containing the repeated str values.
const repeat = require("./repeat");

test("should repeat the string count times", () => {
// case: handle Count of 1:
// When count = 1, return the original string unchanged
test("should return original string when count is 1", () => {
const str = "hello";
const count = 3;
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("hellohellohello");
expect(repeatedStr).toEqual("hello");
});

// case: handle Count of 1:
// 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.

// 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.
// When count = 0, return empty string
test("should return an empty string when count is 0", () => {
const str = "world";
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.
// When count < 0, throw an error
test("should throw an error when count is negative", () => {
const str = "test";
const count = -3;
expect(() => repeat(str, count)).toThrow("Count must be non-negative");
});
Loading