Actually I was working on a logic where I got an array and an input var, I need to compare that var with the array and find the smallest number in that array without using sorting, for eg. I got an array something like
const arr = [20,25,13,10,3,5,9,22];
and an input like
var inputt =3;
So I need to find the smallest number in array but greater then the input in the above situation the expected answer is " 5 ".
What should be the approach, I have tried performing a loop around the array and find the smallest number but cant figure out how to get the smallest number from an array which is greater then the input given.
var myarr = [5,3,9,10,7];
var smallest = arr[0];
for(var i=1; i<arr.length; i++){
if(myarr[i] < smallest){
smallest = myarr[i];
}
}
console.log(smallest);
const arr = [20,25,13,10,3,5,9,22];
const input = 3;
const res = Math.min(...arr.filter(num => num > input));
console.log(res)
Filter out numbers that are less than the input, then find the min of those.