-1

I don't know how to name a title for my question. Here is the problem:

 //Setup all the stats.
        var randomStat = Math.floor(Math.random() * ((monster.level + 5) - monster.level + 1) + monster.level);
        var multiplier = randomStat * itemQuality.qualityMultiplier;


    //Assign the Stats.
    var strength = Math.floor(multiplier * itemSubType.strengthMultiplier / 2); //divide each stat by 2 for better balance
    var endurance = Math.floor(multiplier * itemSubType.enduranceMultiplier / 2);
    var agility = Math.floor(multiplier * itemSubType.agilityMultiplier / 2);
    var dexterity = Math.floor(multiplier * itemSubType.dexterityMultiplier / 2);
    var wisdom = Math.floor(multiplier * itemSubType.wisdomMultiplier / 2);
    var intelligence = Math.floor(multiplier * itemSubType.intelligenceMultiplier / 2);
    var luck = Math.floor(multiplier * itemSubType.luckMultiplier / 2);

What it does, is create a randomStat variable and multiplier variable, then use them for each stat. My problem is that if each stat i.e. "strengthMultiplier" is the same as "enduranceMultiplier" which is the case sometimes, then stats will be exactly the same, because randomStat and multiplier is called just once and is used for every stat.

I am trying to create a loop for it, to call it 7 times, and each time its called use it for a single stat, up to 7 stats total. I could of course create 2 new variables for each stat (total 14) but I hope there is better way to do that, using loops. I am using javascript, so any help is welcome with javascript only, not jquery or anything else. Thanks :)

Mariusz
  • 361
  • 1
  • 4
  • 17
  • So basically, you want a new random multiplier per stat? That alone might not guarantee that no stats are the same, depending of the values. – plalx Jan 25 '15 at 14:20
  • yes I understand, there is a chance they will be the same, but I want as much random stats from each other as possible – Mariusz Jan 25 '15 at 14:23

1 Answers1

0

You might keep your stats in an array or an object (the latter shown here), and iterate over those:

var stats = {
  strenght: null,
  endurance: null,
  agility: null,
  dexterity: null,
  wisdom: null,
  intelligence: null,
  luck: null
};

for (var stat in stats) {
  // todo: consider using Object.hasOwnProperty here.

  var randomStat = Math.floor(Math.random() * ((monster.level + 5) - monster.level + 1) + monster.level);
  var multiplier = randomStat * itemQuality.qualityMultiplier;      

  stats[stat] = Math.floor(multiplier * itemSubType[stat + 'Multiplier'] / 2);
}

That would store values in a stats dict, i.e. as stats.luck instead of luck.

kamituel
  • 34,606
  • 6
  • 81
  • 98
  • Thanks alot :) I dont know much about Object.hasOwnPropety, but i will read it now, and see how to implement this in my code. – Mariusz Jan 25 '15 at 14:32
  • @Mariusz See [this question and answer](http://stackoverflow.com/questions/12735778/for-in-and-hasownproperty) for an explanation why it's a good idea to use `hasOwnProperty`. – kamituel Jan 25 '15 at 14:34
  • Thanks for the link, I will check it out now, I already read some of it and it seems useful. – Mariusz Jan 25 '15 at 14:36