Taken from Sloth Bytes
Given a number of petals, return a string which repeats the phrases
Loves me
andLoves me not
for every alternating petal, and return the last phrase in all caps.Remember to put a comma and space between phrases.
Solution
This is the answer I came up with:
def loves_me(num)
result = []
last = num - 1
num.times { |time|
if time.even?
result << "Loves me"
else
result << "Loves me not"
end
}
result[last] = result[last].upcase
return result.join(", ")
end