Skip to content

Commit 2018056

Browse files
committed
Committed all files on 2-practice-tdd folder
1 parent 842df38 commit 2018056

File tree

6 files changed

+338
-0
lines changed

6 files changed

+338
-0
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,40 @@
1+
/**
2+
* Original file:
3+
*
14
function countChar(stringOfCharacters, findCharacter) {
25
return 5
36
}
47
58
module.exports = countChar;
9+
*
10+
* End of file
11+
*/
12+
13+
// Implementation:
14+
15+
/**
16+
* Counts the number of times a character occurs in a string
17+
* @param {string} str - The string to search
18+
* @param {string} char - The single character to search for
19+
* @returns {number} - The count of occurrences
20+
*/
21+
function countChar(str, char) {
22+
// Input validation
23+
if (typeof str !== 'string' || typeof char !== 'string' || char.length !== 1) {
24+
return 0;
25+
}
26+
27+
let count = 0;
28+
29+
// Loop through each character in the string
30+
for (let i = 0; i < str.length; i++) {
31+
if (str[i] === char) {
32+
count++;
33+
}
34+
}
35+
36+
return count;
37+
}
38+
39+
module.exports = countChar;
40+

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Origignal file:
3+
*
14
// implement a function countChar that counts the number of times a character occurs in a string
25
const countChar = require("./count");
36
// Given a string `str` and a single character `char` to search for,
@@ -22,3 +25,55 @@ test("should count multiple occurrences of a character", () => {
2225
// And a character `char` that does not exist within `str`.
2326
// When the function is called with these inputs,
2427
// Then it should return 0, indicating that no occurrences of `char` were found.
28+
*
29+
* End of file
30+
*/
31+
32+
// Implementation:
33+
34+
const countChar = require("./count");
35+
36+
describe("countChar function", () => {
37+
test("should count multiple occurrences of a character", () => {
38+
const str = "aaaaa";
39+
const char = "a";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(5);
42+
});
43+
44+
test("should return 0 when character does not occur in the string", () => {
45+
const str = "hello world";
46+
const char = "z";
47+
const count = countChar(str, char);
48+
expect(count).toEqual(0);
49+
});
50+
51+
// Additional test cases to ensure robustness
52+
test("should handle empty string", () => {
53+
const str = "";
54+
const char = "a";
55+
const count = countChar(str, char);
56+
expect(count).toEqual(0);
57+
});
58+
59+
test("should handle single occurrence", () => {
60+
const str = "hello world";
61+
const char = "h";
62+
const count = countChar(str, char);
63+
expect(count).toEqual(1);
64+
});
65+
66+
test("should handle case sensitivity", () => {
67+
const str = "Hello World";
68+
const char = "h";
69+
const count = countChar(str, char);
70+
expect(count).toEqual(0); // 'H' is different from 'h'
71+
});
72+
73+
test("should handle invalid inputs gracefully", () => {
74+
expect(countChar(123, "a")).toEqual(0); // Non-string first argument
75+
expect(countChar("hello", 123)).toEqual(0); // Non-string second argument
76+
expect(countChar("hello", "ab")).toEqual(0); // Multiple characters
77+
});
78+
});
79+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1+
/**
2+
* Original file
3+
*
14
function getOrdinalNumber(num) {
25
return "1st";
36
}
47
58
module.exports = getOrdinalNumber;
9+
*
10+
* Enf of file
11+
*/
12+
13+
function getOrdinalNumber(number) {
14+
if (typeof number !== 'number' || !Number.isInteger(number) || number < 0) {
15+
throw new Error('Input must be a non-negative integer');
16+
}
17+
18+
// Special case for numbers ending in 11, 12, 13
19+
const lastTwoDigits = number % 100;
20+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
21+
return number + "th";
22+
}
23+
24+
// Check the last digit
25+
const lastDigit = number % 10;
26+
switch (lastDigit) {
27+
case 1:
28+
return number + "st";
29+
case 2:
30+
return number + "nd";
31+
case 3:
32+
return number + "rd";
33+
default:
34+
return number + "th";
35+
}
36+
}
37+
38+
module.exports = getOrdinalNumber;
39+

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Original file
3+
*
14
const getOrdinalNumber = require("./get-ordinal-number");
25
// In this week's prep, we started implementing getOrdinalNumber.
36
@@ -18,3 +21,107 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1821
expect(getOrdinalNumber(21)).toEqual("21st");
1922
expect(getOrdinalNumber(131)).toEqual("131st");
2023
});
24+
*
25+
* End of file
26+
*/
27+
28+
// Implementation
29+
30+
const getOrdinalNumber = require("./get-ordinal-number");
31+
32+
describe("getOrdinalNumber", () => {
33+
// Case 1: Numbers ending with 1 (but not 11)
34+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
35+
expect(getOrdinalNumber(1)).toEqual("1st");
36+
expect(getOrdinalNumber(21)).toEqual("21st");
37+
expect(getOrdinalNumber(101)).toEqual("101st");
38+
expect(getOrdinalNumber(131)).toEqual("131st");
39+
expect(getOrdinalNumber(1001)).toEqual("1001st");
40+
});
41+
42+
// Case 2: Numbers ending with 2 (but not 12)
43+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
44+
expect(getOrdinalNumber(2)).toEqual("2nd");
45+
expect(getOrdinalNumber(22)).toEqual("22nd");
46+
expect(getOrdinalNumber(102)).toEqual("102nd");
47+
expect(getOrdinalNumber(132)).toEqual("132nd");
48+
expect(getOrdinalNumber(1002)).toEqual("1002nd");
49+
});
50+
51+
// Case 3: Numbers ending with 3 (but not 13)
52+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
53+
expect(getOrdinalNumber(3)).toEqual("3rd");
54+
expect(getOrdinalNumber(23)).toEqual("23rd");
55+
expect(getOrdinalNumber(103)).toEqual("103rd");
56+
expect(getOrdinalNumber(133)).toEqual("133rd");
57+
expect(getOrdinalNumber(1003)).toEqual("1003rd");
58+
});
59+
60+
// Case 4: Numbers ending with 11, 12, or 13
61+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
62+
expect(getOrdinalNumber(11)).toEqual("11th");
63+
expect(getOrdinalNumber(12)).toEqual("12th");
64+
expect(getOrdinalNumber(13)).toEqual("13th");
65+
expect(getOrdinalNumber(111)).toEqual("111th");
66+
expect(getOrdinalNumber(112)).toEqual("112th");
67+
expect(getOrdinalNumber(113)).toEqual("113th");
68+
expect(getOrdinalNumber(1011)).toEqual("1011th");
69+
expect(getOrdinalNumber(1012)).toEqual("1012th");
70+
expect(getOrdinalNumber(1013)).toEqual("1013th");
71+
});
72+
73+
// Case 5: All other numbers (should get "th")
74+
test("should append 'th' for all other numbers", () => {
75+
// Numbers ending with 0
76+
expect(getOrdinalNumber(0)).toEqual("0th");
77+
expect(getOrdinalNumber(10)).toEqual("10th");
78+
expect(getOrdinalNumber(20)).toEqual("20th");
79+
expect(getOrdinalNumber(100)).toEqual("100th");
80+
81+
// Numbers ending with 4-9
82+
expect(getOrdinalNumber(4)).toEqual("4th");
83+
expect(getOrdinalNumber(5)).toEqual("5th");
84+
expect(getOrdinalNumber(6)).toEqual("6th");
85+
expect(getOrdinalNumber(7)).toEqual("7th");
86+
expect(getOrdinalNumber(8)).toEqual("8th");
87+
expect(getOrdinalNumber(9)).toEqual("9th");
88+
89+
// Larger numbers ending with 4-9
90+
expect(getOrdinalNumber(24)).toEqual("24th");
91+
expect(getOrdinalNumber(35)).toEqual("35th");
92+
expect(getOrdinalNumber(46)).toEqual("46th");
93+
expect(getOrdinalNumber(57)).toEqual("57th");
94+
expect(getOrdinalNumber(68)).toEqual("68th");
95+
expect(getOrdinalNumber(79)).toEqual("79th");
96+
});
97+
98+
// Edge cases
99+
describe("edge cases", () => {
100+
test("should handle zero", () => {
101+
expect(getOrdinalNumber(0)).toEqual("0th");
102+
});
103+
104+
test("should handle large numbers", () => {
105+
expect(getOrdinalNumber(1000000)).toEqual("1000000th");
106+
expect(getOrdinalNumber(1000001)).toEqual("1000001st");
107+
expect(getOrdinalNumber(1000002)).toEqual("1000002nd");
108+
expect(getOrdinalNumber(1000003)).toEqual("1000003rd");
109+
expect(getOrdinalNumber(1000011)).toEqual("1000011th");
110+
});
111+
112+
test("should throw error for negative numbers", () => {
113+
expect(() => getOrdinalNumber(-1)).toThrow("Input must be a non-negative integer");
114+
});
115+
116+
test("should throw error for non-integer numbers", () => {
117+
expect(() => getOrdinalNumber(1.5)).toThrow("Input must be a non-negative integer");
118+
});
119+
120+
test("should throw error for non-number inputs", () => {
121+
expect(() => getOrdinalNumber("1")).toThrow("Input must be a non-negative integer");
122+
expect(() => getOrdinalNumber(null)).toThrow("Input must be a non-negative integer");
123+
expect(() => getOrdinalNumber(undefined)).toThrow("Input must be a non-negative integer");
124+
});
125+
});
126+
});
127+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
1+
/**
2+
* Origina file
3+
*
14
function repeatStr() {
25
return "hellohellohello";
36
}
47
58
module.exports = repeatStr;
9+
*
10+
* End of file
11+
*/
12+
13+
/**
14+
* @param {string} str - The string to repeat
15+
* @param {number} count - The number of times to repeat the string (must be non-negative)
16+
* @returns {string} The repeated string
17+
* @throws {Error} When count is negative
18+
*/
19+
function repeatStr(str, count) {
20+
// Input validation
21+
if (typeof str !== 'string') {
22+
throw new Error('First argument must be a string');
23+
}
24+
25+
if (typeof count !== 'number' || !Number.isInteger(count)) {
26+
throw new Error('Count must be an integer');
27+
}
28+
29+
// Handle negative count
30+
if (count < 0) {
31+
throw new Error('Count must be a positive integer');
32+
}
33+
34+
// Handle count of 0
35+
if (count === 0) {
36+
return "";
37+
}
38+
39+
// Use built-in repeat method for efficiency
40+
return str.repeat(count);
41+
}
42+
43+
module.exports = repeatStr;
44+

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Original file:
3+
*
14
// Implement a function repeatStr
25
const repeatStr = require("./repeat-str");
36
// Given a target string `str` and a positive integer `count`,
@@ -30,3 +33,68 @@ test("should repeat the string count times", () => {
3033
// Given a target string `str` and a negative integer `count`,
3134
// When the repeatStr function is called with these inputs,
3235
// Then it should throw an error, as negative counts are not valid.
36+
*
37+
* Enf of file
38+
*/
39+
40+
// Implementation:
41+
42+
const repeatStr = require("./repeat-str");
43+
44+
describe("repeatStr function", () => {
45+
// Test case 1: Multiple repetitions (count > 1)
46+
test("should repeat the string count times when count > 1", () => {
47+
const str = "hello";
48+
const count = 3;
49+
const repeatedStr = repeatStr(str, count);
50+
expect(repeatedStr).toEqual("hellohellohello");
51+
});
52+
53+
// Test case 2: Count equal to 1
54+
test("should return the original string when count is 1", () => {
55+
const str = "hello";
56+
const count = 1;
57+
const repeatedStr = repeatStr(str, count);
58+
expect(repeatedStr).toEqual("hello");
59+
});
60+
61+
// Test case 3: Count equal to 0
62+
test("should return empty string when count is 0", () => {
63+
const str = "hello";
64+
const count = 0;
65+
const repeatedStr = repeatStr(str, count);
66+
expect(repeatedStr).toEqual("");
67+
});
68+
69+
// Test case 4: Negative count
70+
test("should throw an error when count is negative", () => {
71+
const str = "hello";
72+
const count = -1;
73+
expect(() => {
74+
repeatStr(str, count);
75+
}).toThrow("Count must be a positive integer");
76+
});
77+
78+
// Additional test cases for robustness
79+
describe("edge cases", () => {
80+
test("should handle empty string", () => {
81+
expect(repeatStr("", 5)).toEqual("");
82+
});
83+
84+
test("should handle string with spaces", () => {
85+
expect(repeatStr("a b ", 2)).toEqual("a b a b ");
86+
});
87+
88+
test("should throw error when count is not an integer", () => {
89+
expect(() => {
90+
repeatStr("hello", 3.5);
91+
}).toThrow("Count must be an integer");
92+
});
93+
94+
test("should throw error when first argument is not a string", () => {
95+
expect(() => {
96+
repeatStr(123, 3);
97+
}).toThrow("First argument must be a string");
98+
});
99+
});
100+
});

0 commit comments

Comments
 (0)