0

I'm struggling with this small code where I want to look thru an array and fetch the nearest level (number) if experience (2nd number) is between 2 levels.

var Levels = [
   [1, 0],
   [2, 83],
   [3, 174],
   [4, 276],
   [5, 388]
];

var Level = 1;
var Exp = 200;

if(Exp < Levels[1][1])
{
    Level = Levels[Level];
}
else {
    Level = Levels[Level];
}

So the result I should get here is level 3 since I got 200 exp and that will put me between 3-4.

If anyone can help out, I would greatly appreciate it!

Dominik
  • 6,078
  • 8
  • 37
  • 61
  • Does this help you: https://stackoverflow.com/questions/8584902/get-the-closest-number-out-of-an-array – Lemondoge Dec 02 '20 at 21:13
  • Use [`Array.prototype.find()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find): `const getLevel = experience => [...levels].reverse().find(([lvl, exp]) => experience >= exp)[0]` – Yevhen Horbunkov Dec 02 '20 at 21:19
  • None of these works for me, it has to check that its above the required experience and below the next level experience. – ChaoticHex Dec 02 '20 at 22:38

0 Answers0