ECMAScript
ECMAScript

Reputation: 4649

Simple programming math

I'm trying to randomize objects in an object.

do{
    e = enemyList[Math.floor(enemyList.length * Math.random())];
} while (e.level > (player.level + 5) && e.level < (player.level - 5));
return e;

How would I make it so "e" has to be between 5 levels above and 5 levels below in order for the loop to stop?

Yeah this is really easy, but my head hurts for some reason :p

Upvotes: 0

Views: 55

Answers (2)

NPE
NPE

Reputation: 500257

You have the comparisons the wrong way round (+ should be - and vice versa):

} while (e.level > (player.level - 5) && e.level < (player.level + 5));

(The wording of your question is somewhat ambiguous; it could be that you should be using >= and <= instead of > and <.)

Upvotes: 2

Leo Jweda
Leo Jweda

Reputation: 2487

I believe you should be using an or not an and. If it is 5 below OR if it is 5 above. Otherwise the condition will never be met.

Upvotes: 2

Related Questions