I recently encountered a logic problem that involved comparing an input variable with an array to find the smallest number in the array without using sorting. For example, I had an array like:
const arr = [20,25,13,10,3,5,9,22];
and an input value of:
var inputt = 3;
The task was to identify the smallest number in the array that is greater than the given input. In this case, the expected answer would be "5".
I experimented with looping through the array to find the smallest number but struggled to determine how to specifically find the smallest number in the array that is greater than the input.
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);