diff --git a/Go/iteration/repeat.go b/Go/iteration/repeat.go index aecbaf0..1274fdd 100644 --- a/Go/iteration/repeat.go +++ b/Go/iteration/repeat.go @@ -1,10 +1,15 @@ package iteration +import "strings" + func Repeat(char string, numberTimes int) string { + trimmed := strings.Trim(char, " ") + firstChar := strings.Split(trimmed, "") + var repeated string for i := 0; i < numberTimes; i++ { - repeated += char + repeated += firstChar[0] } return repeated diff --git a/Go/iteration/repeat_test.go b/Go/iteration/repeat_test.go index e71218d..3c277ae 100644 --- a/Go/iteration/repeat_test.go +++ b/Go/iteration/repeat_test.go @@ -14,6 +14,24 @@ func TestRepeat(t *testing.T) { t.Errorf("expected %q but got %q", expected, repeated) } }) + + t.Run("should only repeat the first character of a string", func(t *testing.T) { + repeated := Repeat("first", 5) + expected := "fffff" + + if repeated != expected { + t.Errorf("expected %q but got %q", expected, repeated) + } + }) + + t.Run("should trims string", func(t *testing.T) { + repeated := Repeat(" spaaaace ", 2) + expected := "ss" + + if repeated != expected { + t.Errorf("expected %q but got %q", expected, repeated) + } + }) } func BenchmarkRepeat(b *testing.B) {