#Ajax, Callback, and JSON Exercises
##CPS: 1
Rewrite the following code (both the function and the code calling the function) in continuation-passing style:
function square(num) { return num * num; }
var squared = square(5);
##CPS: 1.5
Rewrite the following code (both of the functions and the calling code) in CPS:
function square(num) { return num * num; }
function times2(num) { return num * 2; }
var squared = square(5); var result = times2(squared); console.log('Result is:', result);
##CPS: 2
Rewrite the following code (both of the functions and the calling code) in CPS:
function square(num) { return num * num; }
function squareRoot(num) { return Math.sqrt(num); }
var x = 4; var y = 3; var xSquared = square(x); var ySquared = square(y); var answer = squareRoot(xSquared + ySquared); console.log('The answer is: ' + answer);
##CPS: 3
Add a 1000 millisecond delay to the CPS version of the square function you wrote for the last exercise, and a 500 millisecond delay to the squareRoot function. Re-run the code and see that there's a delay before the answer is printed.
##Ajax Search Form
Download and unzip the ajax_exercise.zip file in a folder. Run it using python server.py and access the app at http://localhost:5000. This is a Flask app. To start, it should render a form at the root URL /. It should also return some JSON result data at /search.
Using jQuery, capture the form submit events:
prevent the default submit action
make an Ajax request to the /search URL using