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

module.exports = countChar;
32 changes: 31 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,41 @@ const countChar = require("./count");
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

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

describe("countChar", () => {
test("counts repeated characters in a simple string", () => {
expect(countChar("aaaaa", "a")).toEqual(5);
});

test("counts characters in a mixed string", () => {
expect(countChar("absankama", "a")).toEqual(4);
});

test("counts characters including spaces", () => {
expect(countChar("ab san kama", "a")).toEqual(4);
});

test("counts characters in string with symbols", () => {
expect(countChar("a-+&£$%?/|b san kama", "a")).toEqual(4);
});

test("returns 0 when character is not found", () => {
expect(countChar("-+&£$%?/|b sn km", "a")).toEqual(0);
});

test("returns 0 for empty string", () => {
expect(countChar("", "a")).toEqual(0);
});

test("is case sensitive", () => {
expect(countChar("AaAa", "a")).toEqual(2);
});
});

// Scenario: No Occurrences
Expand Down
8 changes: 7 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,11 @@
function getOrdinalNumber(num) {
return "1st";
const endDigit = num % 10;
const endTwoDigits = num % 100;
if (endTwoDigits >= 11 && endTwoDigits <= 13) return num + "th";
if (endDigit === 1) return num + "st";
if (endDigit === 2) return num + "nd";
if (endDigit === 3) return num + "rd";
return num + "th";
}

module.exports = getOrdinalNumber;
30 changes: 29 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,36 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
/*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");
});*/

describe("getOrdinalNumber", () => {
test("appends th for numbers ending in 11, 12, or 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});
test("appends st for numbers ending in 1 but not 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(131)).toEqual("131st");
});
test("appends nd for numbers ending in 2 but not 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(932)).toEqual("932nd");
});
test("appends rd for numbers ending in 3 but not 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
});
test("appends th for all other numbers", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(24)).toEqual("24th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function repeatStr() {
function repeatStr(str, count) {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
let combinedStr = "";
if (count < 0) throw new Error("Invalid Count");

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

module.exports = repeatStr;
25 changes: 20 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ const repeatStr = require("./repeat-str");
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.

test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
describe("repeatStr", () => {
test("repeats string count times when count is greater than 1", () => {
expect(repeatStr("hello", 3)).toEqual("hellohellohello");
});

test("returns original string when count is 1", () => {
expect(repeatStr("hello", 1)).toEqual("hello");
});

test("returns empty string when count is 0", () => {
expect(repeatStr("hello", 0)).toEqual("");
});

test("throws an error when count is negative", () => {
expect(() => repeatStr("hello", -3)).toThrow("Invalid Count");
});

test("returns empty string when the input string is empty", () => {
expect(repeatStr("", 3)).toEqual("");
});
});

// Case: handle count of 1:
Expand Down
Loading