the code below is to capitalize all words except for 'littleWords' and the first word of the title. (even if it belongs to the littleWords, the first word should be capitalized.)
def titleize (word)
littleWords = ["and", "the", "over", "or"]
words = Array.new
words = word.split(" ")
titleWords = Array.new
words.each {|word, index|
if index == 0
word = word.capitalize
else
unless littleWords.include?(word)
word = word.capitalize
end
end
titleWords << word
}
return titleWords.join(" ")
end
and test code is the below.
it "does capitalize 'little words' at the start of a title" do
expect(titleize("the bridge over the river chao praya")).to eq("The Bridge over the River chao praya")
end
but it keeps to capitalize the first 'the' as just 'the' instead of 'The'. I wonder which part of my code is wrong. help me... TT
You should use each_with_index instead of each
to get the index