I write something like this in Ruby:
if a.max == a[0]
brand = b[0]
elsif a.max == a[1]
brand = b[1]
elsif a.max == a[2]
brand = b[2]
elsif a.max == a[3]
brand = b[3]
end
a
and b
both are unique arrays.
Is there any way to check all if
and elsif
's in the same condition?
Only one condition for a[0]
, a[1]
, a[2]
and a[3]
?
Array#index might help in cases like these (assuming the size of a
and b
is the same):
brand = b[a.index(a.max)]
In cases in which the array a
might be empty, you will need an additional condition to avoid an error:
index = a.index(a.max)
brand = b[index] if index
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With