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

405
Visualizações
Getting the count of digits from n numbers , timeout error (How many pages in a book? from codewars)

Problem: Given the summary, find the number of pages n the book has.

Example If the input is summary=25, then the output must be n=17: The numbers 1 to 17 have 25 digits in total: 1234567891011121314151617.

All inputs will be valid.

My current solution

function amountOfPages(summary){


    let n=1;   let arrKc=[1]
        
        while((arrKc.join('').toString().length)!=summary){
         
           arrKc.push( n.toString())
         
            n++
        }
        
   return n
  
}

All the tests passes but i get a timeout error as shown below

enter image description here

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

0

Here is my code,

I see a pattern here to find the digits count

9 + 9 * 10 power 1 * 2 + 9 * 10 power 2 * 3 + ...+ 9 * 10 power n-1 * n My solution is based on the above pattern

function initialLoad() {
      document.getElementById("outcome").innerHTML = "Answer: "+  amountOfPages(25);
}

function amountOfPages(summary) {
        var n = summary;
        var totalNumbersSoFar = 0;
        var pagesSoFar = 0;
        var ninthDigit = "9";
        while (parseInt(ninthDigit) < (n / ninthDigit.length)) {
                var numbersInRange = Math.pow(10, ninthDigit.length - 1) * 9;
                pagesSoFar += numbersInRange * ninthDigit.length;
                n -= numbersInRange;
                totalNumbersSoFar += numbersInRange;
                ninthDigit += "9";
        };
        return ((summary - pagesSoFar) / ninthDigit.length) + totalNumbersSoFar;
    };
 <HTML>
        <HEAD>
        </HEAD>
        <BODY id="outcome" onload="initialLoad()">
            <h1>

        </h1>
        </BODY>
    </HTML>

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