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

104
Visualizações
Struggling with this js Codewars challenge

I am learning js and struggling with a problem I encountered on Codewars.

I need to calculate how many numbers containing only binary digits exist between 1 and a random number n. For example, if n is 20, there are 3 such numbers: 1, 10, 11;

I have written a solution that works on codepen but Codewars is telling me it is too inefficient to be accepted. I can't think of anything else I can do to optimize it. Thank you in advance for your help!

function incompleteVirus(n) {
  let countInMemory = 0;
  let isBinary = true;

  // Loop through all numbers below and including n
  for (let i = 1; i <= n; i++) { 

    let strCurrNum = String(i); 

    // Iterate through all digits in the current number
    for (const digit of strCurrNum) {
  
      let numDigit = Number(digit);
 
      // Check if each digit is binary; if not, exit loop
      if (numDigit > 1) {
        isBinary = false;
        break;
      } else {
        isBinary = true;
      }
    }

    // Update memory count
    if (isBinary) {
      countInMemory += 1;
    }
  } 
  return countInMemory
}

EDIT: I am linking the kata here if anyone is interested.

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

0

That was an interesting kata to do, and I had some fun solving it.

I highly suggest you keep trying to solve it by yourself before reading the rest of my answer.

My approach was to check some examples and analyze the digits, rather than the whole numbers.

For example:

  • "10" => "10"
  • "27" => "11"
  • "101" => "101"
  • "102" => "101"
  • "121" => "111"
  • "1095" => "1011"

So here's what I understood from that pattern :

When a digit is higher than 1, replace all the digits on the left with 1s.
Otherwise (if the first digit is 0 or 1) replace any digit that is neither 0 or 1 to 1

So I started building a regular expression.

For the first case, I would need a positive lookbehind : (?<=[2-9].*)\d.
The second case is a simple [^01]

And since for both case, the digits need to be replaced by 1s, only a single replace is needed.

The resulting instruction, including the conversion to decimal, is:

parseInt(n.replace(/(?<=[2-9].*)\d|[^01]/g, 1), 2)
about 4 years ago · Juan Pablo Isaza Relatório

0

@Seblor's answer is the most straight-forward.

But in case you are curious about a solution without regex, here is one option with a more traditional foreach loop:

function bins(n) {
  let exx = false
  let acc = 0
  n.toString().split('').forEach((d, i, arr) => {
    // exx will be true from the moment any digit exceeds 1
    exx ||= d > 1  
    // if the current digit is positive, acc will add up a power of two
    acc += (exx || ~~d) && (2 ** (arr.length - i - 1))
  })
  return acc
}

console.log(bins(1112))

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