JSFiddle - React, Tailwind, and code Playground

by Daniil464

JavaScript

var theArray = [0,30,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500];
var goal = 31;
var closest = null;

$.each(theArray, function(){
  if (this <= goal && (closest == null || (goal - this) < (goal - closest))) {
    closest = this;
  }
});
alert(closest);