//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.
/*function countBs(str) {
var count = 0;
for (var n = 0; n < str.length; n++) {
if (str[n] === "B")
count++;
}
return count;
}
console.log(countBs("BBC News is the Best in the world"));
*/
//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.
function countChar(str, char) {
var count = 0;
for (var n = 0; n < str.length; n++) {
if (str[n] === char)
count++;
}
return count;
}
function countBs(str) {
return countChar(str, "B");
}
console.log(countBs("BBC News is the best in the world"));
console.log(countChar("kakkerlak", "k"));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.