4 ms·
I've never touched Ruby, so I could be misunderstanding, but I think his code is correct. In each loop, he initializes the output variable to an empty string.
by AgentConundrum 14y ago
I've never touched Ruby, so I could be misunderstanding, but I think his code is correct.
In each loop, he initializes the output variable to an empty string. If the current integer is not a multiple, he replaces the empty string with the integer. If it is a multiple, he doesn't know which one it is, or if it's a multiple of both, so he appends the text to the existing string variable. At the end of the loop, the string is output.
Since the `output = i` line is the only assignment that occurs when that condition (not a multiple of either) is met, it makes sense to just reassign the variable rather than append to it. It might be less flexible, sure, but flexibility isn't required here.