Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
38dbbd8
Add test with different string length, it fails
habohlin Oct 14, 2026
6b5df27
Refactor countChar to make prev test pass
habohlin Oct 14, 2026
ffcf1f9
Add test for varying chars in string, it fails
habohlin Oct 14, 2026
9f80255
Refactor countChar to make prev test pass
habohlin Oct 14, 2026
b7fcec6
Add test for any char that exists in string, it fails
habohlin Oct 15, 2026
626f314
Refactor countChar to make prev test pass
habohlin Oct 15, 2026
b184661
Add test for char NOT in string, returns 0, test passes already
habohlin Oct 15, 2026
e59481e
Add test for count is 1, str is 'hello', it fails
habohlin Oct 15, 2026
ce8ef88
Refactor repeatStr to pass prev test
habohlin Oct 15, 2026
3da27c9
Add test for count is 0, str is 'hello', should return empty string, …
habohlin Oct 15, 2026
ece5aaf
Refactor repeatStr to pass prev test
habohlin Oct 15, 2026
41c6f58
Add test for count is 4, test fails
habohlin Oct 15, 2026
c4ed549
Refactor repeatStr to be dynamic, and pass prev test
habohlin Oct 15, 2026
dbb63f0
Add test for count is negative, should throw error, test fails
habohlin Oct 15, 2026
d8bc6e0
Refactor repeatString to throw error and refactor the test, it passes
habohlin Oct 15, 2026
43522a0
Add test for dynamic string value, it fails
habohlin Oct 15, 2026
54e7f2e
Refactor repeatStr to use dynamic string in output, prev test now passes
habohlin Oct 15, 2026
5739486
Refactor function to pass the failing tests
habohlin Oct 15, 2026
6721f24
Add tests for numbers ending with 11, they fail
habohlin Oct 15, 2026
582352f
Refactor function to pass prev tests
habohlin Oct 15, 2026
ed6c46c
Add tests for number ending 2 but not 12, they fail
habohlin Oct 15, 2026
8ad33fc
Refactor function to pass prev tests
habohlin Oct 15, 2026
b21915a
Add tests for numbers ending with 12, they fail
habohlin Oct 15, 2026
4ef1b43
Refactor function to pass prev tests
habohlin Oct 15, 2026
8d41b97
Add tests for numbers ending 3 but not 13, they fail
habohlin Oct 15, 2026
719b29d
Refactor function to pass prev tests
habohlin Oct 15, 2026
c4fa2c6
Add tests for numbers ending 13, they fail
habohlin Oct 15, 2026
5c15ff4
Refactor function to pass prev tests
habohlin Oct 15, 2026
7b78cdd
Add tests for all other whole numbers, they fail
habohlin Oct 15, 2026
eae160f
Refactor function to pass prev tests
habohlin Oct 15, 2026
c032f5b
Add test for throwing error when decimal number passed. Test fails
habohlin Oct 16, 2026
8614766
Refactor function to pass prev test
habohlin Oct 16, 2026
8c54303
Add test for missing argument, it passes on the same premise as the l…
habohlin Oct 16, 2026
74f5c33
Add test for a passed string, it also passes thanks to the same check
habohlin Oct 16, 2026
23e9048
Add test for undefined, it passes as well due to the same reason
habohlin Oct 16, 2026
803ce36
Add test for num is null to throw error, it fails
habohlin Oct 16, 2026
5a8d70f
Refactor function to pass prev test
habohlin Oct 16, 2026
ceaec78
Add test for num is false should throw error, it passes
habohlin Oct 16, 2026
63998f9
Add test for num is true to throw error, it fails
habohlin Oct 16, 2026
0aa534c
Refactor function to pass prev test
habohlin Oct 16, 2026
1724b9a
Add test for num is array to throw error, it fails
habohlin Oct 16, 2026
330bd08
Refactor function to pass prev test
habohlin Oct 16, 2026
9574101
Add test for num is object to throw error, it passes
habohlin Oct 16, 2026
c939e57
Format files with prettier
habohlin Oct 16, 2026
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
10 changes: 9 additions & 1 deletion practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export function countChar(stringOfCharacters, findCharacter) {
return 5
let numOfa = 0;

for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters.charAt(i) === findCharacter) {
numOfa++;
}
}

return numOfa;
}
28 changes: 28 additions & 0 deletions practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,36 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character, any string length", () => {
const str = "aaaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(6);
});

test("any string length, varying chars in string", () => {
const str = "aaabaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(6);
});

test("any string length, varying chars in string, any char in string", () => {
const str = "aaabaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// 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.

test("any string length, varying chars in string, char NOT in string", () => {
const str = "aaabaaa";
const char = "c";
const count = countChar(str, char);
expect(count).toEqual(0);
});
30 changes: 29 additions & 1 deletion practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
export function getOrdinalNumber(num) {
return "1st";
if (num % 1 !== 0 || !num || typeof num !== "number" || num.length > 1) {
throw new Error("Error: whole number is needed");
}

if ((num - 11) % 100 === 0) {
return num + "th";
}

if ((num - 1) % 10 === 0) {
return num + "st";
}

if ((num - 12) % 100 === 0) {
return num + "th";
}

if ((num - 2) % 10 === 0) {
return num + "nd";
}

if ((num - 13) % 100 === 0) {
return num + "th";
}

if ((num - 3) % 10 === 0) {
return num + "rd";
}

return num + "th";
}
73 changes: 73 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,76 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 11. Should append "th" to the number
test("should append 'th' for numbers ending with 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(4211)).toEqual("4211th");
});

// Case 3: Numbers ending with 2 (but not 12). Should append "nd" to the number
test("should 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");
});

// Case 4: Numbers ending with 12. Should append "th" to the number
test("should append 'th' for numbers ending with 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(4212)).toEqual("4212th");
});

// Case 5: Numbers ending with 3 (but not 13). Should append "rd" to the number
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});

// Case 6: Numbers ending with 13. Should append "th" to the number
test("should append 'th' for numbers ending with 12", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(113)).toEqual("113th");
expect(getOrdinalNumber(4213)).toEqual("4213th");
});

// Case 7: All other whole numbers. Should append "th" to the number
test("should append 'th' for all other whole numbers", () => {
expect(getOrdinalNumber(-1834)).toEqual("-1834th");
expect(getOrdinalNumber(23151509)).toEqual("23151509th");
expect(getOrdinalNumber(-100)).toEqual("-100th");
});

// Case 8: Throw error for invalid numbers
test("should throw error for all invalid numbers", () => {
expect(() => {
getOrdinalNumber(5.7);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber();
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber("hey");
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber(undefined);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber(null);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber(false);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber(true);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber([1, 2, 3]);
}).toThrow("Error: whole number is needed");
expect(() => {
getOrdinalNumber({ 4: "hey", bye: 39 });
}).toThrow("Error: whole number is needed");
});
16 changes: 12 additions & 4 deletions practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
export function repeatStr() {
// 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";
export function repeatStr(str, count) {
if (count < 0) {
throw new Error("/Error: count should be non negative integer/");
}

let repeatedStr = "";

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

return repeatedStr;
}
38 changes: 37 additions & 1 deletion practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,60 @@ import { repeatStr } from "./repeat-str.js";
// 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", () => {
test("should repeat the string count times, count is 3", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

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

test("should repeat the string count times, count is 3, any string", () => {
const str = "Orb";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("OrbOrbOrb");
});

// 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 'hello' 1 time", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hello");
});

// 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 empty string, count is 0", () => {
const str = "hello";
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 error, count is negative", () => {
const str = "hello";
const count = -1;
expect(() => {
repeatStr(str, count);
}).toThrow("/Error: count should be non negative integer/");
});
Loading