-3

I am trying to figure out how to make a simple function that will take 2 arguments and return a new string that will repeat the string a certain amount of times that is described in the 2nd argument.

I would like to have it return hellohellohellohellohello but it is only returning hello once.

example:

This is what I have so far, but it only returns hello once

function repeat (arg1, arg2)
{
    var counter = 0
    while(counter < arg2)
        return arg1
        counter++

}
repeat("hello", 5);
Chuck D
  • 1
  • 3

2 Answers2

1

I'm no expert to JS but it took me about 10 seconds to find out this already exists.

"Hello".repeat(10)

Easy.

7H3_H4CK3R
  • 133
  • 8
  • 2
    Small warning though, according to https://developer.mozilla.org/nl/docs/Web/JavaScript/Reference/Global_Objects/String/repeat , it's not yet supported by all browsers. – Me.Name Nov 29 '16 at 20:04
0

I'm not going to give you the code because you should try it yourself. But I will give you the logic to how this will work.

function(arg1, arg2)
{
    counter = 0
    while(counter < arg2)
        print arg1
        counter++

}
SJR59
  • 73
  • 3
  • 14