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
1 change: 1 addition & 0 deletions practice-tdd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 2 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
const occurrences = stringOfCharacters.split(findCharacter);
return occurrences.length - 1
}
47 changes: 39 additions & 8 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,47 @@ import { countChar } from "./count.js";
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// 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 = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

describe("Multiple occurrences", () => {
test("5 'a's in 'aaaaa'", () => {
expect(countChar("aaaaa", "a")).toBe(5);
});
test("2 'l's in 'hello'", () => {
expect(countChar("hello", "l")).toBe(2);
});
test("4 's's in 'mississippi'", () => {
expect(countChar("mississippi", "s")).toBe(4);
});
});

describe("No occurrences", () => {
test("returns 0 for no occurrences", () => {
expect(countChar("hello", "z")).toBe(0);
});
test("returns 0 for empty string", () => {
expect(countChar("", "a")).toBe(0);
});
});

describe("Edge cases", () => {
test("counts when character is at the start", () => {
expect(countChar("aabc", "a")).toBe(2);
});
test("counts when character is at the end", () => {
expect(countChar("abca", "a")).toBe(2);
});
test("counts when character is the whole string", () => {
expect(countChar("aaa", "a")).toBe(3);
});
test("case-sensitive", () => {
expect(countChar("AaA", "A")).toBe(2);
});
test("handles a space character", () => {
expect(countChar("a b c", " ")).toBe(2);
});
});
23 changes: 22 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
export function getOrdinalNumber(num) {
return "1st";
let numSlicedToLastTwo = String(num).slice(-2);
if (numSlicedToLastTwo.length < 2) {
numSlicedToLastTwo = numSlicedToLastTwo.padStart(2, "0")
}
numSlicedToLastTwo = numSlicedToLastTwo
if (String(num).slice(-2) === "11"){
return `${num}th`
}
else if (String(num).slice(-2) === "12"){
return `${num}th`
}
else if (String(num).slice(-2) === "13"){
return `${num}th`
}
if (String(num).slice(-1) === "2") {
return `${num}nd`;
} else if (String(num).slice(-1) === "3") {
return `${num}rd`;
} else if (String(num).slice(-1) === "1") {
return `${num}st`;
}
return `${num}th`
}
27 changes: 27 additions & 0 deletions practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

test("append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

test("append 'th' for numbers ending with 4 - 0, excluding for now those ending with 11, 12, 13", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(25)).toEqual("25th");
expect(getOrdinalNumber(130)).toEqual("130th");
});

test("append 'th' for numbers ending with 11, 12, 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");
});
Loading
Loading