logo CodeStepByStep logo

printStars

Language/Type: JavaScript recursion

Write a recursive function named printStars that accepts an integer parameter n and outputs n occurrences of the "*" character to the console. For example, the call of printStars(5) should output *****.

If passed 0, nothing should be printed. If passed a value less than 0, your function should throw an exception with the message, "argument must be non-negative".

Constraints:

  • Do not declare any global variables.
  • Do not use any loops; you must use recursion.
  • You are allowed to define other "helper" functions if you like; they are subject to these same constraints.
  • Use console.log() to output your result to the console (on a single line).

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.