0

I wrote this code:

$(".awesome").click(function () {
        var toStore = $("input[name=name]").val();
        if (/^[A-Za-z]+ [A-Za-z]+$/.test(toStore)) {
            $("#contain").children().fadeOut(1000);
            $("#contain").delay(1000).queue(function () {
                $("#contain").append("<h1>Welcome to My Quiz : " + toStore + "</br>" +
                    "Youll Get 10 Questions To Answer </br> " +
                    "Here Is the First One:Who is Prime Minister of the United Kingdom? </h1>");

                var allQuestions = [
                    {question: "Who is Prime Minister of the United Kingdom?",
                        choices: ["David Cameron",
                            "Gordon Brown",
                            "Winston Churchill",
                            "Tony Blair"],
                        correctAnswer: 0}
                ];
                $("#contain").append("<form><input type='radio' name='ans1' value='David Cameron'>David Cameron</br>" +
                    " <input type='radio' name='ans1' value='Gordon Brown'>Gordon Brown</br>" +
                    "<input type='radio' name = 'ans1' value = 'Winston Churchill' > Winston Churchill </br>" +
                    "<input type='radio' name='ans1' value='Tony Blair'>Tony Blair</br>" +
                    "<input type='submit' value='Submit' name='submit'></form>");
                $("input[name=submit]").click(function () {
                    var correctAnswer;
                    if ($("input[name=ans1]").val()) {
                        var x = "choices";
                        allQuestions[x] = "David Cameron";
                        for (var x in allQuestions) {
                            return correctAmswer = false;

                            if (allQuestions[x] === "David Cameron") {

                                return correctAnswer = true;
                            } else {
                                return correctAnswer = false;
                            }

                        }
                    }
                });
            });
        } else {
            alert("You Must Put a Valid Name");
        }
    });
});

Now as you can see I have an Object called "allQuestions". Now I have 3 properties: "question" "choices" and "correct answer". Afterwards I have a submit button that if you click it it checks the value of the input name="ans1",then I loop through all of the choices in Object "allQuestions"; if then answer is "David Cameron" I want it to store the correctAnswer to be 1 instead of 0; else I don't want to store the correctAnswer and it remains 0 because I have more questions afterwards. Any suggestions how to do so?

2
  • You have a "return correctAmswer = false;" in the start of your for.. in loop. Is that intended? That assigns the value 'false' to correctAnswer and then returns the value 'false' but correctAnswer won't keep its value afterwards. Commented Apr 7, 2013 at 5:31
  • no it wasnt intended that i wrote correctanmswer,but the code from here:for (var x in allQuestions) { return correctAmswer = false; if (allQuestions[x] === "David Cameron") { return correctAnswer = true; } else { return correctAnswer = false; } is ont seems to me to be a good solution. or am i wrog and im doing it well? Commented Apr 7, 2013 at 5:51

1 Answer 1

1
  • The first problem is that direct event handling does not work with dynamically added elements if you register your event handler before the elements are added. In your case, I think it works because you register you event handler after the elements are added. But you should change it to:

    $("#contain").on("click","input[name=submit]", function(){ your function})

  • The second problem is that the way you access your allQuestions is not correct because it's an array. It should be: allQuestions[0][x];

  • The third problem like @Benjamin Gruenbaum mentioned.

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

2 Comments

can you please explain further more what allQuestions[0][x] does? i see when i put it in console.log that it get's my an array of my choices. meaning containing = ["David Cameron","Gordon Brown","Winston Churchill","Tony Blair"] do i need to do a for in loop in allQuestions[0][x] to check if the value of input name="ans1" is "David Cameron"??
allQuestions[0][x] means: accessing the first question (index = 0) and get the property [x]. In your case: x=choices

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.