Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
get ratio between sum of same char in a string and the whole string length

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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If the statistic as a parameter in ratio function is '', statistic.length is 0. And this causes mathematical error because something cannot be divided by 0. If you write some exceptional code like following, it will work well.

function ratio(statistic) {
   let charcount = 0 ;
   for ( let i = 0 ; i < statistic.length ; i++){
      if ( statistic[i] == 'a' ){ 
         charcount += 1 ;
      }
   }
   return (statistic == ''? 0 : Math.round(charcount/statistic.length * 100)) ;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

when you have empty string or null string, the value of statistic.length will be 0. So you are missing of this check in your code that is why you are getting NaN (Not a Number).

function ratio(statistic) {
 let charcount = 0 ;
 for ( let i = 0 ; i < statistic.length ; i++){
   
   if ( statistic[i] == 'a' ){ charcount += 1 ;}
    
}
// Corrected code here 
return statistic.length == 0 ? 0 : Math.round(charcount/statistic.length * 100) ;
}
console.log(ratio('abababaabaaa'));
console.log(ratio(''));
// 67
// NaN <---- here should be 0

about 4 years ago · Juan Pablo Isaza Relatório

0

Try:

function ratio(statistic) {
  let charcount = 0 ;
  for ( let i = 0; i < statistic.length; i++ ) {
    if ( statistic[i] == 'a' ) { charcount += 1; }
  }
  if (charcount === 0) return 0; // check if there were no a's in the string, if so return 0
  return Math.round(charcount/statistic.length * 100) ;
}
console.log(ratio('bbbbbbbaaa'));
console.log(ratio(''));

To prevent from having to divide by 0, we just check if no a's were found and if so return 0 then and there.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda