Learn to Code in 5 Minutes a Day: Lesson 7
By Code with ADHD
- 2 minutes read - 384 wordsLesson 6 Solution: Roman Numeral I
You should have come up with a solution that looks like the following. If you did, great! All it took was changing:
return ""
to
return “I”
roman.go
func roman(number int) string {
return "I"
}
roman_test.go
func TestRoman(t *testing.T) {
result := roman(1)
expected := "I"
if result != expected {
t.Errorf("result was incorrect, got: %v, want: %v.", result, expected)
}
}
go test
PASS
ok hello 0.472s
If you weren’t able to solve this, go back and retrace it. Remember this isn’t a speed contest. Go back until you understand what we’ve done and how to do it. Feel free to search up details. It’s totally normal to have questions!
Just remember that in real world programming, no one has the answers ahead of time. Just like a writer can’t ask for step by step instructions for how to write a book, a real programmer can’t rely on step by step instructions for programming. You have to rely on your own ability to figure things out to be a successful programmer.
Today’s Lesson: Roman Numeral II
For today’s exercise, let’s take the logical step and see if we can’t update func roman(number int) to handle the number 2.
So you’ll need to write a test where you pass in the number two and get back II. But you have to remember not to break your test that also handles 1 -> I. So it won’t be as simple as “return II”.
Play around with that for a bit. Hint: you will probably need to use the “if” statements you’ve already seen. Think how to write this in code:
If I get the number 1 then give back I, if I get the number 2, then give back II.
Since you’re brand new to programming this might take a little bit of trial and error to get the syntax right. This is totally normal! Once you’ve made your tests pass, check for the next lesson to check your answer.
HINT: write your test first. Once you have a test that tests for two and you run it, you should see something like this failure:
go test
--- FAIL: TestRoman2 (0.00s)
roman_test.go:18: result was incorrect, got: I, want: II.
FAIL
exit status 1
FAIL hello 0.376s