Code:
var some_var = [/*.......*/];
var compare_var;
for (var i = 0; i < some_var.length - 1; i++){
if (some_var[i] <= compare_var && compare_var < some_var[i + 1]){
return some_var[i];
}
}
One value of some_var is 30 and the next one is 40 and the next one is 50 and compare_var is 42.
It wouldn’t return 30 even though it’s less-than 42, because 42 isn’t less-than 40. Which also means that it would return 40 because it’s less-than 42 and 42 is less-than 50.
Edit:
As someone mentioned code translation is not exactly great for SE.so. Just to make it clear: I did try several different things in ruby. I have way more experience in javascript though, so the best way I could think of to phrase my question was to write what I needed out in javascript so that I could see how it translated into ruby. Thanks for the answers
This is a one-liner: