Reputation: 21
I am trying to make a JavaScript calculator for a game. Like most games where you gain experience there is level system.
So for a given experience amount I wish to calculate the level.
Right now I have achieved this the hard way.
if(experience < level2Experience){
level = 1;
} else if(experience >= level2Experience && experience < level3Experience){
level = 2;
} else if(experience >= level3Experience && experience < level4Experience){
level = 3;
}
And so on.
Is there a more concise, less laborious way of achieving the same?
Upvotes: 2
Views: 2204
Reputation: 5363
// levels:
// level 1 => 0 points to 9
// level 2 => 10 points to 19
// ....
var levels = [0,10, 20, 30];
var level = 0;
var experience = 22;
levels.forEach((v, i) => {
if(experience >= v) {
level = i+1;
}
});
console.log(level);
Upvotes: 1
Reputation: 302
I am thinking that the when you are calculating level, based on top of experience, you have a certain kind of a formula in mind. This formula for experience would be based on the kind of tasks that the player performs which gains him experience. Depending on how the amount of experienced required to grow from one level to another changes as the player levels up, there can be 3 possible implementations. 1st and 2nd of which can be automated using some sort of mathematical functions. For example:
1) The experience required to grow from one level to another remains constant as a person levels up. Which in a sense means that if a person has 100 xp, he is level 1, if he has 200 then he is level 2 and so on. This can be converted to a mathematical formula, where levels are simply defined by a function where
level = experience/100
2) The amount of experience required to grow from one level to another changes as a function of certain variables. For example if the required experience to go from level 0 to 1 is 100, but from level 1 to level 2 is 1000. Then we can devise a mathematical formula to calculate the sum of series and see that a person who is at level 1 will have at least 100 or more experience and it will be less than 1000 xp. This can be stored in a function as follows:
var lower_xp_limit = 0
, upper_xp_limit = 0
, level_count = 0;
while(player_xp < upper_xp_limit)
{
lower_xp_limit = 10^level_count*1;
upper_xp_limit = 10^(level_count+2)*1;
level_count++;
}
return level_count-1;
3) In case there are different random experience values which determine the level. Then you can do a simple switch case which breaks or implement a for loop to match the data with an array of elements.
Upvotes: 0
Reputation: 440
There are many approaches you can take to do this. Here are a couple of suggestions.
1) If you simply want to make the code look nicer you can write a helper that resembles this
function isBetween(n, lower, upper) { return n >= lower && n <= upper };
2) If levelxExperience is a limited set and you don't care to optimize it, You can store all levelxExperience in an array like so (where index refers to level)
var expArray = [level2Experience, level3Experience];
then you can get the level by simply iterating from the back like so
var i = expArray.length - 1;
var level;
while ( i >=0 ) {
if (experience > expArray[i]) {
level = i;
break;
}
i--;
}
// console.log(level);
Upvotes: 0