A simple JavaScript quiz

I was going through ashbotandsparki quiz. ( Thanks ahsbotandsparki ! )

I thought I would help out a bit. The updated code can be found in my comment on the above mentioned post. You can also find it here.

What I basically did was:

  • Put all the questions, options, answers, hints in seperate arrays.
  • When the user starts the quiz, I take the first question, the first set of options.
  • If the option selected by the user is the same as the first answer from the array, I proceed loading the next question
  • Else, I show the first hint and start again.
  • I have used jquery a bit.

Let’s execute a pseudo code!

Some of my friends were recently trying out to create a page where the user can drag and drop pieces of pseudo code and they need to check if his pseudo code is fine. The initial solution used was to check the order in which the lines of the pseudo code were placed. But this soon lead to problems, as we know there is no one unique solution to a problem.

What I implemented was to convert the pseudo code into a equivalent JavaScript code ( programatically ) and pass it to eval(). TaDa! It worked!

How (not) to programmatically run a JavaScript code?

Recently I was helping some of my friends who were trying to execute a JavaScript code programmatically. The requirement was that the user can enter a JavaScript code in a textarea and click a button and the user should get the output on the screen. They had taken a longer route: take the code and send it to the server through ajax. Paste the code in a script tag of a dummy page and display the page as an iframe. Wow!

So I pitched in and suggested a simpler solution. Take the code and pass it to the eval(). Ensure you have a try,catch block to capture error messages as well. Worked like a charm! Of course, we had to ‘disable’ a few things in it like alert, prompts, etc. It was fun!