-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13-Day.js
More file actions
26 lines (21 loc) · 743 Bytes
/
13-Day.js
File metadata and controls
26 lines (21 loc) · 743 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
function countLetters(str) {
// Create an empty object to store the letter counts
const counts = {};
// Loop through each character in the string
for (let i = 0; i < str.length; i++) {
const char = str[i];
// Check if the character is already in the counts object
if (counts[char]) {
// If it is, increment the count by 1
counts[char]++;
} else {
// If it's not, add it to the object with a count of 1
counts[char] = 1;
}
}
// Return the counts object
return counts;
}
// Example usage:
const letterCounts = countLetters("JavaScript");
console.log(letterCounts); // Output: { J: 1, a: 2, v: 1, S: 1, c: 1, r: 1, i: 1, p: 1, t: 1 }