<!----------------------------------------- File recursive.html Illustrates a recursive call in JavaScript ------------------------------------------> <html> <head> <title>A recursive function to repeat input (counter version)</title> <script language="JavaScript"> <!-- hide from older browsers //Define function testQuestion() function testQuestion(question, chances) { //Define local variables for the function var answer=eval(question); var output="What is " + question + "?"; var correct='<img src="ok.gif">'; var incorrect='<img src="wrong.gif">'; //Ask the question var response=prompt(output, ""); //Check the result if (chances > 1) { return (response == answer) ? correct : testQuestion(question, chances-1); } else { return (response == answer) ? correct : incorrect; } } // stop hiding --> </script> </head> <body bgcolor="#FFFFFF"> <p> <h2>using a recursive function to repeat input<br> (counter version)</h2> <script language="JavaScript"> <!-- hide from older browsers //Ask the question and return the result var result=testQuestion("10 + 10", 3); document.write(result); // stop hiding --> </script> <p> <hr> </body> </html>