-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeanCounting.js
More file actions
37 lines (35 loc) · 1.22 KB
/
beanCounting.js
File metadata and controls
37 lines (35 loc) · 1.22 KB
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
27
28
29
30
31
32
33
34
35
36
37
/*
You can get the Nth character, or letter, from a string by writing
"string".charAt(N) , similar to how you get its length with
"s".length . The returned value will be a string containing only one
character (for example, "b" ). The first character has position zero,
which causes the last one to be found at position string.length - 1 .
In other words, a two-character string has length 2, and its
characters have positions 0 and 1. Write a function countBs that
takes a string as its only argument and returns a number that
indicates how many uppercase “B” characters are in the string.
Next, write a function called countChar that behaves like countBs,
except it takes a second argument that indicates the character
that is to be counted (rather than counting only uppercase “B”
characters). Rewrite countBs to make use of this new function.
*/
//Part one
function countBs(string) {
count = 0;
for(var i = 0; i < string.length; i++) {
if(string.charAt(i) === 'B') {
count++;
}
}
return count;
}
//Part two
function countChar(string, char) {
count = 0;
for(var i = 0; i < string.length; i++) {
if(string.charAt(i) === char) {
count++;
}
}
return count;
}