[英]How can I get the level by given experience?
I have a formula that calculates the experience based on a certain level and another that calculates the level based on the given experience. 我有一个公式,可以根据一定的水平来计算经验,而另一个公式可以根据给定的经验来计算水平。 But the second function does not return the expected value.
但是第二个函数不返回期望值。
const levels = 40; const xp_for_first_level = 1000; const xp_for_last_level = 1000000; const getExperience = level => { const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels - 1); const A = xp_for_first_level / (Math.exp(B) - 1.0); const old_xp = Math.round(A * Math.exp(B * (level - 1))); const new_xp = Math.round(A * Math.exp(B * level)); return new_xp - old_xp; }; const getLevel = experience => { const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels - 1); const A = xp_for_first_level / (Math.exp(B) - 1.0); return Math.ceil(Math.log(experience / A) / B); }; console.log(getLevel(xp_for_first_level)); // -9 console.log(getLevel(xp_for_last_level)); // 30
Expected result 1 and 40, but returns -9 and 30. 预期结果1和40,但返回-9和30。
Can anyone help? 有人可以帮忙吗?
Anyone enlighten me, please? 有人启发我吗?
I think the formula should change, i tried with this and it given correct return. 我认为公式应该更改,我尝试过此方法,并给出了正确的回报。
const getLevel = experience => {
const B = Math.log(xp_for_last_level / xp_for_first_level) / (levels + 1);
const A = xp_for_first_level - 1;
return Math.ceil(Math.log(experience / A) / B);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.