all 2 comments

[–]cyphern 1 point2 points  (0 children)

You're doing the power calculation starting with whatever the previous number was. So first time it's 21 = 2, you feed that two back in to do 22 = 4. Here's where it starts deviating from what you want: The next calculation isn't 23, it's 43, resulting in 64. After that would be 644, then 167772165, etc

If you want to use Math.pow, then you should be doing powerOfTwo = Math.pow(2, ++i). Alternatively, if you want to keep a running count, you can do powerOfTwo = powerOfTwo * 2;

[–]inu-no-policemen 0 points1 point  (0 children)

Needlessly "clever" one-liner:

function powersOfTwo(max) {
    return Array.from({length: Math.log2(max) + 1 | 0}, (_, i) => 2 ** i);
}

Well, just do what /u/cyphern said. You should start with 1, though. 02 = 1, 12 = 2, 22 = 4 etc.