In my messaging app, I have a level system that track’s a user’s activeness with XP. It doesn’t store the actual level, only the amount of XP they have. Here’s the function for calculating the level:
function calculateLevel(num) {
var lv = 0;
do {
lv++;
} while (num >= Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv)))));
var x = num-Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1))));
var left = Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv))))-num;
var thislv = x + left;
return {
level: lv,
xp: x,
left: left,
next: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv)))),
prev: Math.floor(Math.pow(30, Math.sqrt(Math.sqrt(lv-1)))),
thislv: thislv+1
};
}
The formula for calculating how much XP each level requires is 30√(√(level)). However, I don’t want to use a while loop, because the app starts to slow down a bit after level 60. Is there a way to get the current level without having to use loops?