I am writing up a silly little script to help me assign ratings to movies in my collection. I have an array of movie titles that need sorting. I want to apply a merge-sort-like recursive algorithm to sort the movies by rating. At each comparison, I want a form on the document to be updated. The user will select either A or B -- the better of the two titles -- and then click a "continue" button. It will use this information to proceed with the sort. By the end of the process, the user will have answered A or B to the minimum number of binary comparison questions required to produce an ordered list of the movies.
My problem: how can I have the recursive algorithm wait on the input from the form for each step? Using something like confirm() would allow the code to block while input is determined, but obviously page elements can't do this. Should I involve some nasty timeout function? Keep some kind of global closure? Ideally, I want the "continue" button to be linked to a continueRecursion() callback, but I have no idea how to do this.
Any suggestions on how to attack this?
Stack. (This makes sense, because the programmatic stack is a stack.) So make a stack data structure, and then store emulated local variables representing each function call on each level of the stack. Then put that stack in a global variable - and hopefully you see where this is going ...