SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Quiz Javascript

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quiz Javascript

    So I'm trying to do a quiz.
    Prompt window comes up, asks for a category, then another prompt comes up and asks for a difficulty level.

    Then another prompt comes up and asks the question. After that it is suppose to display the answer and say "hey you were right". And if it's wrong it's suppose to say they were wrong and show the correct answer.


    So that's what I want, but I only manage to make 2 prompt windows pop up, the topic and difficulty...then nothing works for me and I hve no clue from there.

    <script language="javascript" type="text/javascript">

    var topic = prompt("Choose a topic between 1 and 3. #1 Geopraphy, #2 History, #3 Math",0);
    var difficulty = prompt("Choose a difficulty between 1 and 3. #1 Easy, #2 Medium, #3 Hard",0);

    var quiz = new Array(2)
    quiz[1] = new Array(2)
    quiz[1][1] = "question1";
    quiz[1][2] = "question2";
    quiz[1][3] = "question3";

    quiz[2] = new Array(2)
    quiz[2][1] = "question1";
    quiz[2][2] = "question2";
    quiz[2][3] = "question3";

    quiz[3][0] = new Array(2)
    quiz[3][1] = "question1";
    quiz[3][2] = "question2";
    quiz[3][3] = "question3";

    var answer = new Array(2)
    answer[1] = new Array(2)
    answer[1][1] = "answer1";
    answer[1][2] = "answer2";
    answer[1][3] = "answer3";

    answer[2] = new Array(2)
    answer[2][1] = "answer1";
    answer[2][2] = "answer2";
    answer[2][3] = "answer3";

    answer[3] = new Array(2)
    answer[3][1] = "answer1";
    answer[3][2] = "answer2";
    answer[3][3] = "answer3";

    var question = prompt(quiz[topic][difficulty]);


    if (question == answer[topic][difficulty]){
    document.write("Your answer was: " + question + " \n The question was: " + quiz[topic][difficulty]);
    document.write("You were: Right");
    }else{
    document.write("Your answer was: " + question + " \n The question was: " + quiz[topic][difficulty]);
    document.write("You were: Wrong");
    }

    Any help is much appreciated.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. Javascript arrays are zero-based (quiz[0][0] = "something"..etc)
    2. The Array() function uses the number of elements as its parameter (you have 2 but you are adding 3 elements).

    When the user picks the topic, subtract 1 from what they typed in so it corresponds correctly to an array index. same for difficulty.

    Consider defining your arrays like this:

    Code:
    var quiz = [
      ["g_easy","g_med","g_hard"],
      ["h_easy","h_med","h_hard"],
      ["m_easy","m_med","m_hard"]
    ];


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •