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

271
Visualizações
How to divide a number by 2 as many times as possible in Javascript?

I would like to write a program that reads an integer and then divides it by 2 as many times as possible while writing the number as a product of two numbers multiplied by a number that is no longer divisible by 2.

For example:

I would like an integer: 120

120 = 2 * 2 * 2 * 15

Here is as far as I have gotten (updated based the well-meaning comments but still not perfect):

let num = Number(prompt('The number: '));

let i = 0;
while(!(num % 2)) { 
    num /= 2; 
    i++; 
}

let solution = Array(i).fill(2).join(' * ');

console.log(solution);

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You are almost there - you got how to get how many 2 you need - now just need to add the remaining num onto it - and maybe remember the total:

let solution = Array(i).fill(2).join(' * ') + " * " + num;

Full solution:

let num = Number(prompt('The number: '));
let total = num; // remember for later

let i = 0;
while(!(num % 2)) { 
    num /= 2; 
    i++; 
}

let solution = total + " = " + Array(i).fill(2).join(' * ') + " * " + num;

console.log(solution);

about 4 years ago · Santiago Gelvez 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