Learn to Code In 5 Minutes A Day: Lesson 14
By Code with ADHD
- 2 minutes read - 258 wordsSolution for IX and X
Does your solution look like this:
package main
func roman(number int) string {
result := ""
if number >= 10 {
result = "X"
number = number - 10
}
if number >= 9 {
result = "IX"
number = number - 9
}
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 your tests look like this?
func TestRoman9(t *testing.T) {
result := roman(9)
expected := "IX"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
func TestRoman10(t *testing.T) {
result := roman(10)
expected := "X"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
Do you understand everything you’ve done to get to this point?
Do you notice that a lot of this is looking repetitive?
There’s one more key change we will end up making, so… for now my recommendation is just to notice that there is repetition.
Any time there is repetition, we can think about refactoring our logic. But refactoring is an art. If you refactor too soon, you might miss the bigger pattern. Never refactor and you build a hard to maintain mess.
Today’s Lesson: 11, 12, 13
Can you write tests for 11, 12, and 13?
Can you make the code work to pass the tests?