+ 1

How can i write a function to count amount of uppercase "B" in a string using just one argument

Javascript function

2nd Feb 2019, 2:38 PM
Udojike Nmesoma
Udojike Nmesoma - avatar
3 odpowiedzi
+ 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));
2nd Feb 2019, 3:43 PM
Ipang
+ 2
function countB(text) { let count = 0; for (let char of text) { if(char === 'B') count++; } return count; } console.log(countB('ABbaBaBaB'));
2nd Feb 2019, 2:52 PM
Random
Random - avatar
+ 2
let str = "AbABbB"; let arr = str.match(/B/g); console.log(arr.length);
2nd Feb 2019, 6:34 PM
Suraj Jha
Suraj Jha - avatar