我想为一个数字分配一个级别,如下所示:
如果数字介于 3 和 3+3^2 之间,Level
应为 2。
如果数字介于 3+3^2 和 3+3^2+3^3 之间,Level
应为 3。
如果数字介于 3+3^2+3^3 和 3+3^2+3^3+3^4 之间,Level
应为 4。
....
等等...
我正在尝试这个..
var level = (next_slot>3 && next_slot < 3+3**2)?1:(
(next_slot>3+3**2 && next_slot < 3+3**2+3+3**3)?2:(
next_slot>3+3**2+3**3 && next_slot < 3+3**2+3+3**3)?3:(
4
))
这看起来很复杂,而且没有限制。
有没有更好的方法来解决这个问题?
最佳答案
使用对数,卢克:
level = Math.ceil(Math.log(x) / Math.log(3))
关于javascript - 如何检查一个数字是否位于数学级数的两个成员之间?例如A.P.、G.P.或任何其他进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52182977/