I've created a formula that can calculate experience based on specific levels and another formula that calculates the level based on given experience. However, there seems to be an issue with the second function as it is not returning the expected value.
const totalLevels = 40;
const xpForFirstLevel = 1000;
const xpForLastLevel = 1000000;
const getExperienceValue = level => {
const BValue = Math.log(xpForLastLevel / xpForFirstLevel) / (totalLevels - 1);
const AValue = xpForFirstLevel / (Math.exp(BValue) - 1.0);
const oldXp = Math.round(AValue * Math.exp(BValue * (level - 1)));
const newExp = Math.round(AValue * Math.exp(BValue * level));
return newExp - oldXp;
};
const getLevelBasedOnExp = experience => {
const BValue = Math.log(xpForLastLevel / xpForFirstLevel) / (totalLevels - 1);
const AValue = xpForFirstLevel / (Math.exp(BValue) - 1.0);
return Math.ceil(Math.log(experience / AValue) / BValue);
};
console.log(getLevelBasedOnExp(xpForFirstLevel)); // -9
console.log(getLevelBasedOnExp(xpForLastLevel)); // 30
The expected results are supposed to be 1 and 40, however, the actual output is -9 and 30.
Is there anyone who can provide assistance?
Can someone offer insight into this matter?