logo CodeStepByStep logo

zigzag

Language/Type: JavaScript recursion

Write a recursive function zigzag that accepts an integer parameter n and that outputs to the console n characters as follows:
The middle character of the output should always be an asterisk ("*"). If you are asked to write out an even number of characters, then there will be two asterisks in the middle ("**"). Before the asterisk(s) you should write out less-than characters ("<"). After the asterisk(s) you should write out greater-than characters (">"). For example, the following calls produce the following output:

Call Output
zigzag(1) *
zigzag(2) **
zigzag(3) <*>
zigzag(4) <**>
zigzag(5) <<*>>
zigzag(6) <<**>>
zigzag(7) <<<*>>>
zigzag(8) <<<**>>>

Your function should throw an exception with the message, "argument must be greater than or equal to 1", if passed a value less than 1. Note that the output does not advance to the next line. Use console.log() to output your final result to the console (note that only one line is printed for a single call to the function).

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.