Drill

ProblemsJavaScript › games

Look up a player level

mediumgamesJavaScript

A game tracks experience points against a table of ascending thresholds. Find the highest level reached.

levelProgress(xp: int, thresholds: list<int>) → int

Solve it in the editor →

Where you start

function levelProgress(xp, thresholds) {
  
}

Worked examples

CallResult
levelProgress(50, [10,30,60,100])1
levelProgress(100, [10,30,60,100])3
levelProgress(5, [10,30,60])-1
levelProgress(0, [])0

Hint

Walk the thresholds and remember the last one that was met.

Reference solution in JavaScript
function levelProgress(xp, thresholds) {
  if (thresholds.length === 0) return 0;
  if (xp < thresholds[0]) return -1;
  let result = 0;
  for (let i = 0; i < thresholds.length; i++) {
    if (thresholds[i] <= xp) result = i;
  }
  return result;
}

The same problem in another language

More games problems in JavaScript