-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathcount.js
More file actions
26 lines (22 loc) · 846 Bytes
/
count.js
File metadata and controls
26 lines (22 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* TDD Practice: Count Characters
* -----------------------------
* Goal: Implement a function that counts how many times a character appears in a string.
* This version uses a classic 'for...of' loop for maximum clarity.
*/
function countChar(stringOfCharacters, findCharacter) {
// 1. Initialize a counter starting from 0
let totalCount = 0;
// 2. Loop through every character in the input string
for (let currentCharacter of stringOfCharacters) {
// 3. Check if the current character matches the character we are looking for
if (currentCharacter === findCharacter) {
// 4. If they match, increment the counter by 1
totalCount++;
}
}
// 5. Return the final count after finishing the loop
return totalCount;
}
// Exporting the function so the test file can access it
module.exports = countChar;