-
-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathcount.js
More file actions
22 lines (20 loc) · 716 Bytes
/
Copy pathcount.js
File metadata and controls
22 lines (20 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Counts how many times a single character appears in a string.
*
* @param {string} stringOfCharacters - The string to search through.
* @param {string} findCharacter - The single character to count.
* @returns {number} The number of times findCharacter appears in stringOfCharacters.
*
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
*/
function countChar(stringOfCharacters, findCharacter) {
let count = 0;
for (const char of stringOfCharacters) {
if (char === findCharacter) {
count++; // this means count = count + 1. This term is called
// "incrementing" the count variable.
}
}
return count;
}
module.exports = countChar;