logo CodeStepByStep logo

hash_string

Language/Type: PHP recursion

Write a recursive function named hash_string that accepts an integer parameter $k and returns a String of # (hash symbols) that is 2k characters long (i.e., 2 to the $kth power). For example, the call of hash_string(3) returns "########".

If passed a value for $k less than 0, your function should throw an Exception with the message, "Error: argument must be non-negative."

Constraints:

  • Do not use any loops; you must use recursion.
Function: Write a PHP 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.