iteration: add repeatTime as an argument
This commit is contained in:
parent
2684521bdd
commit
75304d4c23
@ -1,9 +1,7 @@
|
||||
package iteration
|
||||
|
||||
const repeatTime = 5
|
||||
|
||||
// Repeat takes a string and repeat it 5 times
|
||||
func Repeat(s string) string {
|
||||
func Repeat(s string, repeatTime int) string {
|
||||
var res string
|
||||
for i := 0; i < repeatTime; i++ {
|
||||
res += s
|
||||
|
@ -6,13 +6,13 @@ import (
|
||||
)
|
||||
|
||||
func ExampleRepeat() {
|
||||
repeated := Repeat("ab")
|
||||
repeated := Repeat("ab", 5)
|
||||
fmt.Println(repeated)
|
||||
// Output: ababababab
|
||||
}
|
||||
|
||||
func TestRepeat(t *testing.T) {
|
||||
repeated := Repeat("a")
|
||||
repeated := Repeat("a", 5)
|
||||
expected := "aaaaa"
|
||||
|
||||
if repeated != expected {
|
||||
@ -22,6 +22,6 @@ func TestRepeat(t *testing.T) {
|
||||
|
||||
func BenchmarkRepeat(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Repeat("a")
|
||||
Repeat("a", 5)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user