Click here to Skip to main content
15,902,275 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function called repeat, which accepts a string and a number and returns a new string with the string repeated that number of times.

Do not use the built in repeat method - the tests will fail if you do!

Examples:

repeat('Matt', 3) // 'MattMattMatt'
repeat('Elie', 2) // 'ElieElie'
repeat('Michael', 0) // ''

What I have tried:

function repeat(str, num) {
return (str) * num;
}
Posted
Updated 8-May-20 22:26pm

This is one of possible algorithms:
  • Initialize a string variable as empty string.
  • Loop a number of times equal to second parameter.
    • Append first parameter to variable.
  • Return variable.
 
Share this answer
 
Quote:
How can I fix this code?

In order to learn JS, you have to read documentation and do some search by yourself.
Multiply do not work for strings, you have to search other way.
Trial and error is how you learn, from your mistakes.
This is a nice tutorial site: JavaScript Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900