NOTE: there are other solutions that work. These are just our proposed solutions.
E1: The Road Not Taken
def reverse(array)
new_array = []
index = -1
array.each do
new_array << array[index]
index -= 1
end
new_array
end
# OR
def reverse(array)
length = array.length
reversed_array = []
while length > 0
reversed_array << array[length - 1]
length -= 1
end
reversed_array
end
E2: Custom FizzBuzz
def fizzbuzz(max_val)
1.upto max_val do |num|
if num % 15 == 0
puts "fizzbuzz"
elsif num % 5 == 0
puts "buzz"
elsif num % 3 == 0
puts "fizz"
else
puts num
end
end
end
# OR
def fizzbuzz(max_val)
num = 1
while num <= max_val
if num % 15 == 0
puts "fizzbuzz"
elsif num % 5 == 0
puts "buzz"
elsif num % 3 == 0
puts "fizz"
else
puts num
end
num += 1
end
end
## BONUS: what's the return value of the top method?
## BONUS: what's the return value of the bottom method?
E3: Google Map
engines = ["Google", "Bing", "Ask Jeeves"]
result = engines.map do |e|
if e == "Google"
"OK"
elsif e == "Bing"
"Bad!"
else
"What is that?"
end
end