P. Nick
P. Nick

Reputation: 991

Reversing game experience formula to show how much experience a level needs

I have a formula where I can calculate the character level for my game based on how much experience they have, like so:

const calculateLevel = experience => {
    return ((Math.sqrt(625+100 * experience)-25) / 50);
}

console.log("50 XP is Level: " + calculateLevel(50));
console.log("150 XP is Level: " + calculateLevel(150));
console.log("659 XP is Level: " + calculateLevel(659));
console.log("3268 XP is Level: " + calculateLevel(3268));

But how can I reverse engineer this and calculate how much experience they have, using their level only?

Upvotes: 0

Views: 255

Answers (1)

OmG
OmG

Reputation: 18838

So you need to revert the specified function:

y = (sqrt(625 + 100 * x) - 25)/50 =>
x = ((50 * y + 25)^2 - 625) / 100

const calculateExperience = level => {
    return (Math.pow(50 * level + 25, 2) - 625) / 100;
}

console.log("Level 1 is Experience: " + calculate_experience(1));
console.log("Level 2 is Experience: " + calculate_experience(2));

Upvotes: 2

Related Questions