Skip to content

Instantly share code, notes, and snippets.

@jonurry
Last active July 7, 2023 10:44
Show Gist options
  • Save jonurry/ef3d28f16ba1e6ec6693a0777af52ad9 to your computer and use it in GitHub Desktop.
Save jonurry/ef3d28f16ba1e6ec6693a0777af52ad9 to your computer and use it in GitHub Desktop.
3.3 Bean Counting (Eloquent JavaScript Solutions)
function countBs(s) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === "B")
count += 1;
}
return count;
}
function countChar(s, c) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === c)
count += 1;
}
return count;
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4
@CaoLamBaoKhanh
Copy link

function countChar(s, c) {
var count = 0;
for (var i = 0; i < s.length; i += 1) {
if (s.charAt(i) === c)
count += 1;
}
return count;
}
function countBs(s){
return countChar(s,"B");
}
console.log(countBs("BBC"));
// → 2
console.log(countChar("kakkerlak", "k"));
// → 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment