Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
Check whether every item in an array is a power of 2

I can't seem to put this problem together,

Return an array containing all indices that are powers of 2

What I have below so far, I know modulo is probably the wrong operate, but I was trying to make it work. I'm not sure what else to try here.

function secondPower(arr) {
   
    let newarr = [];
  for(let i = 0; i < arr.length; i++){
    if(arr[i] / (2 ** i) === 1){
      newarr.push(i);
       }
  }
  return newarr;
}

[1, 2, 4, 5, 7, 16]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use Math.log2 to check whether the number is a power of 2, and Array.every to check whether every item in the array matches a condition:

function isPowerOf2(x) {
    return Math.log2(x) % 1 === 0;
}

const arr = [1, 2, 4, 5, 7, 16]
const arr2 = [2, 4, 8]

function isArrAllPowerOf2(arr){
  return arr.every(isPowerOf2)
}

console.log(isArrAllPowerOf2(arr))
console.log(isArrAllPowerOf2(arr2))

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda