iteration: add repeatTime as an argument

This commit is contained in:
vinchent 2024-09-10 13:32:46 +02:00
parent 2684521bdd
commit 75304d4c23
2 changed files with 4 additions and 6 deletions

View File

@ -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

View File

@ -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)
}
}