this function should count the "a" in a string and calculate a ratio between the sum of all "a" and the string length. It works fine except when the string is empty and it should output "0" instead of NaN. That'
function ratio(statistic) {
let charcount = 0 ;
for ( let i = 0 ; i < statistic.length ; i++){
if ( statistic[i] == 'a' ){ charcount += 1 ;}
}
return Math.round(charcount/statistic.length * 100) ;
}
console.log(ratio('abababaabaaa'));
console.log(ratio(''));
// 67
// NaN <---- here should be 0
s what I want to fix, thank you.