Skip to content

Commit eac7d73

Browse files
committed
update count.js
1 parent 124ae45 commit eac7d73

File tree

6 files changed

+129
-55
lines changed

6 files changed

+129
-55
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3-
}
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
48

9+
return count;
10+
}
511
module.exports = countChar;
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1+
// ═══════════════════════════════════════════════════════
2+
// 🎯 Personal Implementation: countChar Function Tests
3+
4+
// Purpose: Test-first practice for counting character occurrences
5+
// Style: Gherkin-style BDD comments + Jest assertions
6+
// ═══════════════════════════════════════════════════════
7+
18
// implement a function countChar that counts the number of times a character occurs in a string
29
const countChar = require("./count");
10+
11+
// ───────────────────────────────────────────────────────
12+
// 📋 Specification:
313
// Given a string `str` and a single character `char` to search for,
414
// When the countChar function is called with these inputs,
5-
// Then it should:
15+
// Then it should return the number of times `char` appears in `str`.
16+
// ───────────────────────────────────────────────────────
17+
18+
// ═══════════════════════════════════════════════════════
19+
// 🧪 Test Scenarios
20+
// ═══════════════════════════════════════════════════════
621

722
// Scenario: Multiple Occurrences
23+
// ──────────────────────────────
824
// Given the input string `str`,
925
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
1026
// When the function is called with these inputs,
1127
// Then it should correctly count occurrences of `char`.
12-
1328
test("should count multiple occurrences of a character", () => {
14-
const str = "aaaaa";
15-
const char = "a";
29+
const str = "aaaaa"; // 🎯 Input: repeated character
30+
const char = "a"; // 🔍 Search for: 'a'
1631
const count = countChar(str, char);
17-
expect(count).toEqual(5);
32+
expect(count).toEqual(5); // ✅ Expect: 5 occurrences
1833
});
1934

2035
// Scenario: No Occurrences
36+
// ─────────────────────────
2137
// Given the input string `str`,
2238
// And a character `char` that does not exist within `str`.
2339
// When the function is called with these inputs,
2440
// Then it should return 0, indicating that no occurrences of `char` were found.
41+
test("should return 0 for no occurence", () => {
42+
const str = "asdf"; // 🎯 Input: no 'l' present
43+
const char = "l"; // 🔍 Search for: 'l' (not in string)
44+
const count = countChar(str, char);
45+
expect(count).toEqual(0); // ✅ Expect: 0 occurrences
46+
});
47+
48+
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
let numberToString = String(num);
3+
let numberLastDigit = numberToString.slice(-1);
4+
let numberLast2Digits = numberToString.slice(-2);
5+
6+
if (numberLastDigit === "1" && numberLast2Digits !== "11")
7+
return numberToString + "st";
8+
if (numberLastDigit === "2" && numberLast2Digits !== "12")
9+
return numberToString + "nd";
10+
if (numberLastDigit === "3" && numberLast2Digits !== "13")
11+
return numberToString + "rd";
12+
return numberToString + "th";
313
}
414

5-
module.exports = getOrdinalNumber;
15+
16+
module.exports = getOrdinalNumber;
Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
1+
12
const getOrdinalNumber = require("./get-ordinal-number");
2-
// In this week's prep, we started implementing getOrdinalNumber.
3-
4-
// Continue testing and implementing getOrdinalNumber for additional cases.
5-
// Write your tests using Jest — remember to run your tests often for continual feedback.
6-
7-
// To ensure thorough testing, we need broad scenarios that cover all possible cases.
8-
// Listing individual values, however, can quickly lead to an unmanageable number of test cases.
9-
// Instead of writing tests for individual numbers, consider grouping all possible input values
10-
// into meaningful categories. Then, select representative samples from each category to test.
11-
// This approach improves coverage and makes our tests easier to maintain.
12-
13-
// Case 1: Numbers ending with 1 (but not 11)
14-
// When the number ends with 1, except those ending with 11,
15-
// Then the function should return a string by appending "st" to the number.
16-
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
3+
4+
// ───────────────────────────────────────────────────────
5+
// 📋 Rule Summary:
6+
// • 1 → "1st", 2 → "2nd", 3 → "3rd" (unless teen)
7+
// • 11, 12, 13 → always "th" (teen exception)
8+
// • All others → last digit determines suffix
9+
// ───────────────────────────────────────────────────────
10+
11+
// ── Case 1: Numbers ending in 1 (excluding teens) → "st" ──
12+
test("appends 'st' to numbers ending in 1, except 11", () => {
13+
expect(getOrdinalNumber(1)).toEqual("1st"); // single digit
14+
expect(getOrdinalNumber(21)).toEqual("21st"); // double digit
15+
expect(getOrdinalNumber(131)).toEqual("131st");// triple digit
16+
});
17+
18+
// ── Case 2: Numbers ending in 2 (excluding teens) → "nd" ──
19+
test("appends 'nd' to numbers ending in 2, except 12", () => {
20+
expect(getOrdinalNumber(2)).toEqual("2nd");
21+
expect(getOrdinalNumber(22)).toEqual("22nd");
22+
expect(getOrdinalNumber(122)).toEqual("122nd");
23+
});
24+
25+
// ── Case 3: Numbers ending in 3 (excluding teens) → "rd" ──
26+
test("appends 'rd' to numbers ending in 3, except 13", () => {
27+
expect(getOrdinalNumber(3)).toEqual("3rd");
28+
expect(getOrdinalNumber(23)).toEqual("23rd");
29+
expect(getOrdinalNumber(123)).toEqual("123rd");
30+
});
31+
32+
// ── Case 4: Default "th" suffix (teens + digits 0,4-9) ──
33+
test("appends 'th' for teens (11-13) and digits 0,4-9", () => {
34+
// Teen exceptions (critical edge case)
35+
expect(getOrdinalNumber(11)).toEqual("11th");
36+
expect(getOrdinalNumber(12)).toEqual("12th");
37+
expect(getOrdinalNumber(13)).toEqual("13th");
38+
expect(getOrdinalNumber(111)).toEqual("111th"); // larger teen
39+
40+
// Default suffix digits
41+
expect(getOrdinalNumber(4)).toEqual("4th");
42+
expect(getOrdinalNumber(10)).toEqual("10th");
43+
expect(getOrdinalNumber(25)).toEqual("25th");
2044
});
45+
46+
47+
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count < 0) throw new Error("negative counts are not valid");
3+
return str.repeat(count);
34
}
45

5-
module.exports = repeatStr;
6+
module.exports = repeatStr;

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

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
// Implement a function repeatStr
21
const repeatStr = require("./repeat-str");
3-
// Given a target string `str` and a positive integer `count`,
4-
// When the repeatStr function is called with these inputs,
5-
// Then it should:
62

7-
// Case: handle multiple repetitions:
8-
// Given a target string `str` and a positive integer `count` greater than 1,
9-
// When the repeatStr function is called with these inputs,
10-
// Then it should return a string that contains the original `str` repeated `count` times.
3+
// ───────────────────────────────────────────────────────
4+
// Rule Summary:
5+
// • count > 1 → str repeated count times ("hi", 3 → "hihihi")
6+
// • count = 1 → original string unchanged
7+
// • count = 0 → empty string ""
8+
// • count < 0 → throw error (invalid input)
9+
// ───────────────────────────────────────────────────────
1110

12-
test("should repeat the string count times", () => {
13-
const str = "hello";
14-
const count = 3;
15-
const repeatedStr = repeatStr(str, count);
16-
expect(repeatedStr).toEqual("hellohellohello");
11+
// ── Case 1: Multiple repetitions (count > 1) ──
12+
test("repeats string correctly for count > 1", () => {
13+
expect(repeatStr("hello", 3)).toEqual("hellohellohello");
14+
expect(repeatStr("a", 5)).toEqual("aaaaa");
15+
expect(repeatStr("xy", 2)).toEqual("xyxy");
1716
});
1817

19-
// Case: handle count of 1:
20-
// Given a target string `str` and a `count` equal to 1,
21-
// When the repeatStr function is called with these inputs,
22-
// Then it should return the original `str` without repetition.
18+
// ── Case 2: Single repetition (count = 1) ──
19+
test("returns original string unchanged when count = 1", () => {
20+
expect(repeatStr("bye", 1)).toEqual("bye");
21+
expect(repeatStr("x", 1)).toEqual("x");
22+
expect(repeatStr("test", 1)).toEqual("test");
23+
});
24+
25+
// ── Case 3: Zero repetition (count = 0) ──
26+
test("returns empty string when count = 0", () => {
27+
expect(repeatStr("no", 0)).toEqual("");
28+
expect(repeatStr("anything", 0)).toEqual("");
29+
expect(repeatStr("", 0)).toEqual("");
30+
});
2331

24-
// Case: Handle count of 0:
25-
// Given a target string `str` and a `count` equal to 0,
26-
// When the repeatStr function is called with these inputs,
27-
// Then it should return an empty string.
32+
// ── Case 4: Invalid input (negative count) ──
33+
test("throws error for negative count values", () => {
34+
expect(() => repeatStr("str", -1)).toThrow("negative counts are not valid");
35+
expect(() => repeatStr("x", -10)).toThrow("negative counts are not valid");
36+
});
2837

29-
// Case: Handle negative count:
30-
// Given a target string `str` and a negative integer `count`,
31-
// When the repeatStr function is called with these inputs,
32-
// Then it should throw an error, as negative counts are not valid.

0 commit comments

Comments
 (0)