+ 1
How can i write a function to count amount of uppercase "B" in a string using just one argument
Javascript function
3 Answers
+ 4
Pass the string and the char to count in an array.
function one_arg(arg) {
var count = 0;
var pos = -1;
while((pos = arg[0].indexOf(arg[1], pos + 1)) !== -1) {
count++;
}
return count;
}
var a = ["Big brown baby bear", "b"];
console.log(one_arg(a));
+ 2
function countB(text) {
let count = 0;
for (let char of text) {
if(char === 'B')
count++;
}
return count;
}
console.log(countB('ABbaBaBaB'));
+ 2
let str = "AbABbB";
let arr = str.match(/B/g);
console.log(arr.length);