this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

:sparkles: (Go) trimming and spliting string in Repeat function

- added test in case string is a word
- added test in case string has space

+24 -1
+6 -1
Go/iteration/repeat.go
··· 1 1 package iteration 2 2 3 + import "strings" 4 + 3 5 func Repeat(char string, numberTimes int) string { 6 + trimmed := strings.Trim(char, " ") 7 + firstChar := strings.Split(trimmed, "") 8 + 4 9 var repeated string 5 10 6 11 for i := 0; i < numberTimes; i++ { 7 - repeated += char 12 + repeated += firstChar[0] 8 13 } 9 14 10 15 return repeated
+18
Go/iteration/repeat_test.go
··· 14 14 t.Errorf("expected %q but got %q", expected, repeated) 15 15 } 16 16 }) 17 + 18 + t.Run("should only repeat the first character of a string", func(t *testing.T) { 19 + repeated := Repeat("first", 5) 20 + expected := "fffff" 21 + 22 + if repeated != expected { 23 + t.Errorf("expected %q but got %q", expected, repeated) 24 + } 25 + }) 26 + 27 + t.Run("should trims string", func(t *testing.T) { 28 + repeated := Repeat(" spaaaace ", 2) 29 + expected := "ss" 30 + 31 + if repeated != expected { 32 + t.Errorf("expected %q but got %q", expected, repeated) 33 + } 34 + }) 17 35 } 18 36 19 37 func BenchmarkRepeat(b *testing.B) {