Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created December 19, 2020 10:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save codecademydev/ae986e50c18c13438fcf3f11ed417a1c to your computer and use it in GitHub Desktop.
Codecademy export
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++) {
number = arr[i];
// The 'inner' while loop - searches for smallest power of 2 greater than the given number
let j = 1;
while (j < number) {
j = j * 2;
}
results.push(j);
}
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