Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 13, 2020 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/593d22a1ea5bd0d87e57a2a2b2eea608 to your computer and use it in GitHub Desktop.
Save codecademydev/593d22a1ea5bd0d87e57a2a2b2eea608 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++) {
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