Possible duplicate:What is the opposite of Math.pow JavaScript?
2^x=i
Given i, how can we calculate x using Javascript?
You want to take the logarithm of 8000. JS has a function Math.logthat uses base e, you want base 2so you can write Math.log(8000) / Math.log(2)to get the logarithm of 8000 base 2, which is equal to x.
Math.log
e
2
Math.log(8000) / Math.log(2)
You need the logarithm from the Math object. It does not provide a 2 log base, so do the conversion:
var x = Math.log(8000) / Math.log(2);
Math javascript object reference.
In a more general case, we calculate 2 ^ x = i as follows:
var i; // Some number var x = Math.log(i) / Math.log(2);