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
12 changes: 11 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
let position = stringOfCharacters.indexOf(findCharacter);
if (position === -1) {
return 0;
} else {
while (position !== -1) {
count++;
position = stringOfCharacters.indexOf(findCharacter, position + 1);
}
return count;
}
}
35 changes: 35 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,48 @@ import { countChar } from "./count.js";
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

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

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 count multiple occurrences of a character", () => {
const str = "Shirin";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count multiple occurrences of a character", () => {
const str = "This ia a second string test";
const char = "s";
const count = countChar(str, char);
expect(count).toEqual(4);
});

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

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

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down
24 changes: 23 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
export function getOrdinalNumber(num) {
return "1st";
if (num % 100 === 11) {
throw new Error("Input must not ending with 11");
}
if (num % 10 === 1) {
return `${num}st`;
}

if (num % 100 === 22) {
throw new Error("Input must not ending with 22");
}

if (num % 10 === 2) {
return `${num}nd`;
}
if (num % 100 === 33) {
throw new Error("Input must not ending with 33");
}

if (num % 10 === 3) {
return `${num}rd`;
}

return `${num}th`;
}
50 changes: 50 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getOrdinalNumber } from "./get-ordinal-number.js";
import { test, expect } from "@jest/globals";
// In this week's prep, we started implementing getOrdinalNumber.

// Continue testing and implementing getOrdinalNumber for additional cases.
Expand All @@ -11,10 +12,59 @@ import { getOrdinalNumber } from "./get-ordinal-number.js";
// This approach improves coverage and makes our tests easier to maintain.

// Case 1: Numbers ending with 1 (but not 11)
test("should not append 'st' for number ending with 11 ", () => {
expect(() => getOrdinalNumber(11)).toThrow("Input must not ending with 11");
expect(() => getOrdinalNumber(111)).toThrow("Input must not ending with 11");
expect(() => getOrdinalNumber(45711)).toThrow(
"Input must not ending with 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", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(241)).toEqual("241st");
});

// Case 2: Numbers ending with 2 (but not 22)
// When the number ends with 2, except those ending with 22,
// Then the function should return a string by appending "nd" to the number.
test("should append 'nd' for numbers ending with 2, except those ending with 22", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(32)).toEqual("32nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

test("should not append 'nd' for number ending with 22 ", () => {
expect(() => getOrdinalNumber(22)).toThrow("Input must not ending with 22");
expect(() => getOrdinalNumber(122)).toThrow("Input must not ending with 22");
expect(() => getOrdinalNumber(45722)).toThrow(
"Input must not ending with 22"
);
});

// Case 3: Numbers ending with 3 (but not 33)
test("should append 'rd' for numbers ending with 3, except those ending with 33", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(13)).toEqual("13rd");
expect(getOrdinalNumber(123)).toEqual("123rd");
});

test("should not append 'rd' for number ending with 33 ", () => {
expect(() => getOrdinalNumber(33)).toThrow("Input must not ending with 33");
expect(() => getOrdinalNumber(133)).toThrow("Input must not ending with 33");
expect(() => getOrdinalNumber(45733)).toThrow(
"Input must not ending with 33"
);
});

// Case 3: Numbers ending in other digits
test("should append 'th' for numbers ending in other digits", () => {
expect(getOrdinalNumber(4)).toBe("4th");
expect(getOrdinalNumber(5)).toBe("5th");
expect(getOrdinalNumber(10)).toBe("10th");
expect(getOrdinalNumber(20)).toBe("20th");
});
11 changes: 9 additions & 2 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export function repeatStr() {
export 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";
if (count < 0) {
throw new Error("negative counts are not valid");
}
let strRep = "";
for (let i = 0; i < count; i++) {
strRep += str;
}
return strRep;
}
28 changes: 25 additions & 3 deletions practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Implement a function repeatStr
import { repeatStr } from "./repeat-str.js";
import { test, expect } from "@jest/globals";
// Given a target string `str` and a positive integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should:
Expand All @@ -15,18 +16,39 @@ test("should repeat the string count times", () => {
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("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.

test("should repeat the string count times", () => {
const str = "World";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("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.

test("should return an empty string for zero", () => {
const str = "Hi!";
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 throw an error, as negative counts are not valid", () => {
const str = "Hi!";
const count = -4;
expect(() => repeatStr(str, -4)).toThrow("negative counts are not valid");
});

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