The formula that appeared in today's drill.
def search(target_num, input)
input.each_with_index do |num, index|
if num == target_num
puts "#{index + 1}Is in the second"
return
end
end
puts "That number is not included"
end
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
search(12, input)
Now you can output "4th".
On the other hand
def search(target_num, input)
input.each_with_index do |num, index|
if num == target_num
puts "#{index + 1}Is in the second"
return
else
puts "That number is not included"
next
end
end
end
input = [3, 5, 9 ,12, 15, 21, 29, 35, 42, 51, 62, 78, 81, 87, 92, 93]
This is using next, The processing in the if statement continues until the fourth 12 appears.
The result is the same without next, Around this time, I thought it would be better to have next in terms of readability.
There was a lot of discussion about this description until before noon. It may be said that it is one of the attractions of programming.
Recommended Posts