JSFiddle - React, Tailwind, and code Playground

by zachpainter77

JavaScript

function func(array,val){
      return array.sort((a,b) => b - a)
      	.find((a,i,x) => (x[i] !== x[i-1]-1) && (x[i] <= val || i === x.length-1));
      console.log(val);
    }
    console.log(func([3,5,6],6));
    console.log(func([4,5,6],6));