0

I am trying to make a quiz for a school a friend of mine works. The students get to see 4 word and 4 sentences and they need to drag the right word to the sentence that fits with it.

I am making this inside the browser with jQuery and MySQL, this so that the teacher is able to view who did the assignment and who didn't.

The teacher want's to have 4 questions per page and then you click the next button and you see what you did wrong/good and move on to the next 4 questions. At the end I need to write it to the database.

Currently I am walking through the answer with the following jQuery code.

 if ($("#answer1").data("answer") == $("#question1").text()) {
   $("#question1").css("background","#0F9");
   good++;
 } else {
   $("#question1").css("background","#903");
   mistakes++;
 };

I was thinking about loading all the questions at once and hiding/showing them when you click the next button to prevent double questions and to make it easy to remember the good/wrong answers.

Now I want to make the looping through the code above dynamic. So I though about putting it in a for look like this.

 for (var i = 0; i < 4; i++) {
   if ($("#answer" & (i+page)).data("answer") == $("#question" & (i+page)).text()) {
     $("#question" & (i+page)).css("background","#0F9");
     good++;
   } else {
     $("#question" & (i+page)).css("background","#903");
     mistakes++;
   }; 
 }

Ofcourse the & (i+page) part isn't working at all. (the page increases when you hit the next button by 4). Is there some way to make this possible in javascript/jQuery or do I need to chance my approach completely?

8
  • What is 'page' a reference to? Are you trying to concatenate #answer + i + page into a selector string? Commented Nov 15, 2013 at 18:40
  • 3
    Off-topic, but: $('[id^=question]').each(function() {$(this).text($('#answer'+this.id.replace(/\D/g,'')).data("answer"));}); Run in the console, this code instantly answers all questions correctly. NEVER store the answers in the browser. Commented Nov 15, 2013 at 18:40
  • Can the student just do a "view source" on this quiz and see all the right answers? Commented Nov 15, 2013 at 18:40
  • 1
    @NiettheDarkAbsol you probably just ruined the day of the one kid in the class that knows how to view source Commented Nov 15, 2013 at 18:41
  • Try + instead of & Commented Nov 15, 2013 at 18:41

2 Answers 2

4

Replace all & with + in the code you gave.

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

Comments

2

String concatenation in JavaScript is done with the + operator. The & is a bitwise AND.

JavaScript also has

'foo' + 0; // "foo0" i.e. String + Number = String
0 + 'foo'; // "0foo" i.e. Number + String = String

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.