Learn to Code In 5 Minutes A Day: Lesson 13
By Code with ADHD
- 2 minutes read - 221 wordsLesson 12 Solution
So yesterday you worked on converting 6, 7, and 8 to VI, VII, and VIII.
Does your solution look like this?
romans_test.go:
func TestRoman6(t *testing.T) {
result := roman(6)
expected := "VI"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
func TestRoman7(t *testing.T) {
result := roman(7)
expected := "VII"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
func TestRoman8(t *testing.T) {
result := roman(8)
expected := "VIII"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
romans.go
func roman(number int) string {
result := ""
if number >= 5 {
result = "V"
number = number - 5
}
if number == 4 {
result = "IV"
number = number - 4
}
for i := 0; i < number; i++ {
result += "I"
}
return result
}
Do you see the one character change in the solution that made this work?
This is key, so again, if you don’t understand everything, keep working at it. Feel free to start back over and see if you can get to this same point.
Lesson 13: IX and X
Can you write the tests for 9 and 10 and solve both of them?