15 doesn't work and 9 is broken
Like I said, 15 doesn’t work and the test for 9 breaks:
I suspect if you’ve been following along in Excel then you’ve discovered you too make lots of little mistakes as you go. At least I hope so and that it isn’t just me. It is one of the nice things about all those tests … when you break something by making a simple mistake, the tests tell you, you fix it quickly and cheaply, and move on.
When I see that the 9 test case is also broken it is immediately obvious what I’ve done wrong.
I fix it quickly. The tests work.
Public Function i2r(i As Integer) As String
Application.Volatile
i2r = ""
If i = 9 Then
i2r = "IX"
i = i - 9
End If
If i >= 10 Then
i2r = i2r + "X"
i = i - 10
End If
If i >= 5 Then
i2r = i2r + "V"
i = i - 5
End If
If i = 4 Then
i2r = i2r + "IV"
End If
While i <= 3 And i > 0
i2r = i2r + "I"
i = i - 1
Wend
End Function
Then I do a bit of tidying up – moving things around a little.
For each small change I make, I recalc the tests.
Public Function i2r(i As Integer) As String
Application.Volatile
i2r = ""
If i >= 10 Then
i2r = i2r + "X"
i = i - 10
End If
If i = 9 Then
i2r = i2r + "IX"
i = i - 9
End If
If i >= 5 Then
i2r = i2r + "V"
i = i - 5
End If
If i = 4 Then
i2r = i2r + "IV"
End If
While i <= 3 And i > 0
i2r = i2r + "I"
i = i - 1
Wend
End Function
Then I do a bit more tidying up.
Public Function i2r(i As Integer) As String
Application.Volatile
i2r = ""
If i >= 10 Then
i2r = i2r + "X"
i = i - 10
End If
If i = 9 Then
i2r = i2r + "IX"
i = i - 9
End If
If i >= 5 Then
i2r = i2r + "V"
i = i - 5
End If
If i = 4 Then
i2r = i2r + "IV"
i = i - 4
End If
While i >= 1
i2r = i2r + "I"
i = i - 1
Wend
End Function
Then I look at the code and I see the obvious pattern.