Javascript refactor if condition function
Basically we search for the most close number from array where our number belongs. After we find the min value, we take the index of it and add 1, because the rank starts from 1



//rank 1 : 150 - 500
//rank 2 : 500 - 1000
//rank 3 : 1000 - 2000
//rank 4 : 2000 - 3500
//rank 5 : 3500 - 5000
//rank 6 : 5000 - 5500

let pointsRank = [150, 500, 1000, 2000, 3500, 5000, 5500]

function rank(pointsRank, score) {
  var closest = pointsRank.reduce(function(prev, curr) {
    return (Math.abs(curr - score) &lt; Math.abs(prev - score) ? curr : prev);
  });
  var rank = pointsRank.indexOf(closest) + 1; // cuz starts from 0 and we don't have rank 0
  console.log("min range: " + closest +
    " | our score: " + score +
    " | max range: " + pointsRank[rank])

  return {
    rank: rank,
    pointsLeftForNextRank: (pointsRank[rank] - score)
  }

}

const test1 = rank(pointsRank, 1200);
const test2 = rank(pointsRank, 2000);
const test3 = rank(pointsRank, 3700);

console.log(test1);
console.log(test2);
console.log(test3);