Walkthrough
Solutions
def capitalize_each_word(str)
words = str.split
capitalized_words = []
words.each do |word|
capitalized_words << word.capitalize
end
capitalized_words.join(" ")
end
def compute_squares(arr)
squares = []
arr.each do |element|
squares << element ** 2
end
squares
end
def two_sum_to_zero?(arr)
arr.each_index do |idx|
arr_without_el = arr[0...idx] + arr[idx+1...arr.length]
return true if arr_without_el.include?(arr[idx] * -1)
end
false
end
def longest_word(str)
words = str.split
longest_word = ""
words.each do |word|
if word.length > longest_word.length
longest_word = word
end
end
longest_word
end
def capitalize_each_word(str)
arr = str.split
i = 0
while i < arr.length
arr[i] = arr[i].capitalize
i += 1
end
arr.join(" ")
end
def compute_squares(arr)
i = 0
while i < arr.length
arr[i] = arr[i] ** 2
i += 1
end
arr
end
def two_sum_to_zero?(arr)
i = 0
while i < arr.length
others = arr[0...i] + arr[(i+1)..-1]
return true if others.include?(0 - arr[i])
i += 1
end
false
end
def longest_word(str)
arr = str.split
longest = arr.first
i = 1
while i < arr.length
if arr[i].length > longest.length
longest = arr[i]
end
i += 1
end
longest
end