1

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?

1
  • The data structure that is used to represent recursion is the 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 ... Commented Jun 15, 2012 at 19:32

1 Answer 1

1

Just store the result between function calls and have the user be the one who initiates the routine.

So:

  1. Get data
  2. Present choice
  3. Upon user's selection, give present choice + old data to sort routine.
  4. After sort present data to step 1 and continue again.

You'll just need to hold the data in a global or keep passing it around.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.