I'm currently developing a character generator that determines your score based on the experience points you allocate to it. The scoring system is such that 1 XP gives you a score of 1, 3 XP gives you a score of 2, 6 XP gives you a score of 3, 10 XP gives you a score of 4, and so on.
My issue lies in trying to figure out a straightforward method to calculate that if I have 10 XP in a skill, the associated score should be 4. Likewise, if I have 105 XP in the skill, the score should be 14.
There's also a multiplier of 1.5 which allows users to purchase skills for less than the default XP requirement. For instance, instead of needing 3 points for a score of 2, they would only need 2. Similarly, to achieve a score of 6, only 4 points are necessary.
Strangely enough, my code seems to function well up until values surpassing 10. At that point, the required points seem to spike more rapidly than anticipated.
Users would simply click on an input field, type or increment the number, initiating the following calculation:
getScoreFromXP(xp) {
const xpMultiplier: number = 1.5
const calcXP: number = xp * xpMultiplier;
return this.getScoreFromXPCalc(calcXP);
}
getScoreFromXPCalc(xp) {
let val: number = 0;
while (xp > val) {
val++;
xp = xp - val;
}
return val;
}
What can I do to improve this calculation and ensure it operates as intended?