Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
Comprobar si cada elemento de una matriz es una potencia de 2

Parece que no puedo resolver este problema,

Devuelve una matriz que contiene todos los índices que son potencias de 2

Lo que tengo a continuación hasta ahora, sé que módulo es probablemente el funcionamiento incorrecto, pero estaba tratando de hacerlo funcionar. No estoy seguro de qué más probar aquí.

 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 answers
Answer question

0

Puede usar Math.log2 para verificar si el número es una potencia de 2 y Array.every para verificar si todos los elementos de la matriz coinciden con una condición:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!