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

module.exports = countChar;
module.exports = countChar;
35 changes: 34 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// implement a function countChar that counts the number of times a character occurs in a string
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:
Expand All @@ -9,13 +9,46 @@ const countChar = require("./count");
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
// 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').

const countChar = require('./count');


test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});
test("should return 0 when the character is not found", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Case Sensitivity
// It should be case-sensitive, meaning 'A' != 'a'.
test("should be case-sensitive when counting characters", () => {
const str = "Banana";
expect(countChar(str, "a")).toEqual(3);
expect(countChar(str, "A")).toEqual(0);
});

// Scenario: Empty String
// It should return 0 when the input string is empty.
test("should return 0 when the input string is empty", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Special Characters
// It should correctly count spaces and punctuation.
test("should count special characters like spaces or punctuation", () => {
expect(countChar("a b a b", " ")).toEqual(3);
expect(countChar("wow!!!", "!")).toEqual(3);
});

// Scenario: No Occurrences
// Given the input string str,
Expand Down
21 changes: 20 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,24 @@

function getOrdinalNumber(num) {
return "1st";
const remainder10 = num % 10;
const remainder100 = num % 100;

if (remainder100 >= 11 && remainder100 <= 13) {
return `${num}th`;
}

switch (remainder10) {
case 1:
return `${num}st`;
case 2:
return `${num}nd`;
case 3:
return `${num}rd`;
default:
return `${num}th`;
}
}

module.exports = getOrdinalNumber;


41 changes: 39 additions & 2 deletions 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.

Have you tried running this test script to make sure it can run properly?

Copy link
Author

Choose a reason for hiding this comment

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

Now I have made sure that the tests have all passed.

Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const getOrdinalNumber = require("./get-ordinal-number");


module.exports = getOrdinalNumber;

// In this week's prep, we started implementing getOrdinalNumber

// continue testing and implementing getOrdinalNumber for additional cases
Expand All @@ -8,6 +12,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");

describe("getOrdinalNumber()", () => {
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toBe("1st");
});

test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toBe("2nd");
});

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toBe("3rd");
});

test("should return '4th' for 4", () => {
expect(getOrdinalNumber(4)).toBe("4th");
});

test("should return '11th', '12th', '13th' for special cases", () => {
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
});

test("should return correct suffixes for 21, 22, 23", () => {
expect(getOrdinalNumber(21)).toBe("21st");
expect(getOrdinalNumber(22)).toBe("22nd");
expect(getOrdinalNumber(23)).toBe("23rd");
});

test("should return '111th' for numbers ending with 11, 12, 13 even if larger", () => {
expect(getOrdinalNumber(111)).toBe("111th");
expect(getOrdinalNumber(112)).toBe("112th");
expect(getOrdinalNumber(113)).toBe("113th");
});
});
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 must be a non-negative integer");
}
return str.repeat(count);
}

module.exports = repeatStr;
38 changes: 13 additions & 25 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");
// Given a target string str and a positive integer count,
// When the repeatStr function is called with these inputs,
// Then it should:

// case: repeat String:
// Given a target string str and a positive integer count,
// When the repeatStr 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 repeatStr = require("./repeat-str");

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

// case: handle Count of 1:
// 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, ensuring that a count of 1 results in no repetition.
test("should return the original string when count is 1", () => {
expect(repeatStr("world", 1)).toBe("world");
});

// 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, ensuring that a count of 0 results in an empty output.
test("should return an empty string when count is 0", () => {
expect(repeatStr("test", 0)).toBe("");
});

test("should throw an error when count is negative", () => {
expect(() => repeatStr("oops", -2)).toThrow("Count must be a non-negative integer");
});

// case: 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 or return an appropriate error message, as negative counts are not valid.