-
-
Save codecademydev/593d22a1ea5bd0d87e57a2a2b2eea608 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numbers = [5, 3, 9, 30]; | |
const smallestPowerOfTwo = arr => { | |
let results = []; | |
// The 'outer' for loop - loops through each element in the array | |
for (let i = 0; i < arr.length; i++) { | |
let number = arr[i]; | |
// The 'inner' while loop - searches for smallest power of 2 greater than the given number | |
let k = 2; | |
while (k < number) { | |
k = k * 2; | |
} | |
results.push(k); | |
} | |
return results | |
} | |
console.log(smallestPowerOfTwo(numbers)) | |
// Should print the returned array [ 8, 4, 16, 32 ] instead prints the returned array [8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment