0

console.log( ‘blah’.repeatMe( 3 ) );

Using Javascript write the code that would make the previous function print:

Output: blahblahblah

4
  • 1
    Sorry, no free codez. Commented Mar 19, 2015 at 0:07
  • What have you tried? What did not work? Can you do it without the syntactic sugar of attaching the method to strings directly? Commented Mar 19, 2015 at 0:10
  • Sounds like homework to me. What have you tried? Where did you get stuck? Can you ask a more specific question about a piece of the solution rather than just ask us to write the code for you? Commented Mar 19, 2015 at 0:10
  • Hey guys, this was an exercise I saw into a test. I ask how to solve it in order to find anyone who is interested in just shows up this solution for fun. Commented Mar 19, 2015 at 14:19

1 Answer 1

1

Oh, it's too fun to pass up a functional style solution.

String.prototype.repeatMe = function(n) {
    if (n <= 0) return "";
    if (n%2 === 1) return (""+this) + this.repeatMe(n-1);

    var half = this.repeatMe(n/2);
    return half + half;
}

document.body.innerHTML = "tester".repeat(10)

I'll let you work out what's happening as an exercise.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Lye. It's very fun to analize your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.