1

Im still working on my Text RPG, but I dont have any easy way to write my level up code... Heres what i got:

var LEVEL = 1;
var XP = 0;
var ATTACK = 0;
var STRENGTH = 0;
var DEFENSE = 0;
var RANGED = 0;
var MAGIC = 0;
var AGILITY= 0;
var HEALTH = 0;

if(LEVEL = 1 && XP >= 20) {
    LEVEL = 2;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 2 && XP >= 60) {
    LEVEL = 3;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 3 && XP >= 120) {
    LEVEL = 4;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 4 && XP >= 240) {
    LEVEL = 5;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 5 && XP >= 480) {
    LEVEL = 6;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 6 && XP >= 960) {
    LEVEL = 7;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 7 && XP >= 1920) {
    LEVEL = 8;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 8 && XP >= 3840) {
    LEVEL = 9;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 9 && XP >= 7680) {
    LEVEL = 10;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 10 && XP >= 15360) {
    LEVEL = 11;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 11 && XP >= 30720) {
    LEVEL = 12;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 12 && XP >= 61440) {
    LEVEL = 13;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 13 && XP >= 122880) {
    LEVEL = 14;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 14 && XP >= 245760) {
    LEVEL = 15;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 15 && XP >= 491520) {
    LEVEL = 16;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 16 && XP >= 983040) {
    LEVEL = 17;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 17 && XP >= 1966080) {
    LEVEL = 18;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 18 && XP >= 3932160) {
    LEVEL = 19;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

} else if(LEVEL = 19 && XP >= 7864320) {
    LEVEL = 20;
    ATTACK++;
    STRENGTH++;
    DEFENCE++;
    RANGED++;
    MAGIC++;
    AGILITY++;

}

I dont know if I could use a for() or a while() loop, but if anyone could help me, that would save ALOT of space and time! (btw, my xp required for each other level is doubled)

WarrenT
  • 4,502
  • 19
  • 27
  • make one named function that "++"'s all of them, and just repeat the function name. – dandavis May 30 '15 at 21:49
  • 1
    You need to use `==` inside an `if` statement, *not* `=`. – Frxstrem May 30 '15 at 21:50
  • You could store the XP cutoffs in an array, then check the XP against the array, using the current level as the index. Then, regardless of the current level, you can increment everything at once if the user has the required XP. – elixenide May 30 '15 at 21:51
  • I'd start by lowercasing the variables, it would help with readability. Uppercase vars are a convention for constants, but yours are mutable. – elclanrs May 30 '15 at 21:51
  • Thanks for the help! –  May 30 '15 at 21:54
  • Reading tag descriptions will help you pick the right tags. Since this site is for professional (and avid) programmers, tags often represent languages or technologies, such as RPG: "RPG is a high-level programming language (HLL) for business applications, initials which stand for Report Program Generator. IBM is the creator and primary vendor of RPG..." – WarrenT Jun 01 '15 at 18:24

3 Answers3

2

You could have a single function with a levels parameter to store the XP values at which to increment the level, e.g.

var incrementLevels = (function (){
  var levels = {1:20,2:60,3:120,4:240};  // and so on…
  return function() {
    if (XP >= levels[LEVEL]) {
      LEVEL++;
      ATTACK++;
      STRENGTH++;
      DEFENCE++;
      RANGED++;
      MAGIC++;
      AGILITY++;
    }
  }
}());

I agree with elclanrs that using lower case for the variables is much preferred.

Edit

The above creates a function called incrementLevels that's called like:

incrementLevels();

It uses the global variables you've assigned. It would be better to put those in an object, say called powers (or abilities or whatever):

var powers = {xp: 0, level: 1, attack: 0, strength: 0, 
              defence: 0, ranged: 0, magic: 0, agility: 0};

then rather than setting values as globals, set values of properties of the object and pass it to the incrementLevels function as required. It could then be:

var incrementLevels = (function() {
  var levels = {1:20, 2:60, 3:120, 4:240 };  // and so on…
  var powerList = ['level','attack','strength','defence','ranged','magic','agility'];

  return function(powers) {
    if (powers.xp >= levels[powers.level]) {
      powerList.forEach(function (p){powers[p]++});
    }
  }
}());

And call it:

incrementLevels(powers);

You can have multiple powers objects, say powersA, powersB that are associated with other objects.

Note that the brackets () wrapping the function aren't strictly necessary, but are important to indicate that the function is an immediately invoked function expression (IIFE) before you get to the bottom and see the final (). Also see Why use (function(){})() or !function(){}()?.

Some exmaples:

powers.xp = 30;
incrementLevels(powers);
console.log(JSON.stringify(powers));
// {"xp":30,"level":2,"attack":1,"strength":1,"defence":1,"ranged":1,"magic":1,"agility":1}

powers.xp = 128;
incrementLevels(powers);
console.log(JSON.stringify(powers));
// {"xp":128,"level":3,"attack":2,"strength":2,"defence":2,"ranged":2,"magic":2,"agility":2}
Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209
  • Thanks but, I have a few questions- What is the point in using the 1:20, - the : in particular? –  May 30 '15 at 22:02
  • It's the syntax for an [*Object initialiser*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) or literal (also [ECMA-262 §11.1.5](http://ecma-international.org/ecma-262/5.1/#sec-11.1.5)). – RobG May 30 '15 at 22:03
  • And once the function is done, would you get rid of the () right inbetween the } ); ??? –  May 30 '15 at 22:06
1

You are repeating the same 6 lines of code inside each if-statement. This is a perfect example of when to use a function. This gives us code reuse and much better readability.

Do this instead:

NOTE: you should be using triple equals for comparison in the if-statement.

if(LEVEL === 1 && XP >= 20){
 LEVEL = 2;
 incrementStats();
}

function incrementStats(){
  ATTACK++;
  STRENGTH++;
  DEFENCE++;
  RANGED++;
  MAGIC++;
  AGILITY++;
}

Now you can simply call incrementStats() in each if-statement, rather than type it all out each time.

Danny Delott
  • 6,756
  • 3
  • 33
  • 57
-1
var LEVEL = 1;
var XP = 0;
var ATTACK = 0;
var STRENGTH = 0;
var DEFENSE = 0;
var RANGED = 0;
var MAGIC = 0;
var AGILITY = 0;
var HEALTH = 0;

function xpUp(xp){
    if (LEVEL !== countLevel(xp, 1)) {
        LEVEL++;
        ATTACK++;
        STRENGTH++;
        DEFENSE++;
        RANGED++;
        MAGIC++;
        AGILITY++;
        HEALTH++;
    }
    console.log(LEVEL);
}

function countLevel(xp, level){
    if (Math.floor(xp / 60) > 0)
        return countLevel(xp / 2, ++level);
    else
        return level;
}
Microshine
  • 731
  • 5
  • 19
  • That is a bit more advanced for my level of coding, but Thanks!! –  May 30 '15 at 22:08
  • If `XP == 30` and `LEVEL == 1`, this will not increment the level when it should. Also, answers should have an explanation, not just code. – RobG May 30 '15 at 22:15
  • @RobG author asked for refactoring, not for explanation. If you want to start level 1 from 30xp, just replace 60 to 30 in countLevel condition – Microshine May 30 '15 at 22:19
  • Actually I asked if anybody could help me just in general, explaination or just a few tips, but anything to help is better than nothing! :P –  May 30 '15 at 23:16