logo CodeStepByStep logo

repeat

Language/Type: JavaScript arrays

Write a function named repeat that accepts an array of Strings and an integer k as parameters and that replaces each element with k copies of that element.

For example, if an array stores the values ["how", "R", "you?"] before the function is called and k is 3, it should store the values ["how", "how", "how", "R", "R", "R", "you?", "you?", "you?"] after the function finishes executing. If k is 0 or negative, the array should be empty after the call. You may assume that the array passed is non-null.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.