Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Displaying all the arrays in a variable

    Hey,

    Say I've got a variable with a load of arrays in it ...

    Code:
    questions = [
    
    ["<Question1> Question",
        "choice1","choice2", "choice3"],
    ["<Question2> Question",
        "choice1","choice2", "choice3"],
    ["<Question3> Question",
        "choice1","choice2", "choice3"],
    ["<Question4> Question",
        "choice1","choice2", "choice3"],
    ...
    ["<Question11>   Question",
        "choice1","choice2", "choice3"],
    ...
    ]
    And in a textbox called 'ShowAllQuestions' I want to list all the questions in order (but without showing the choices (answers) along with them). How would i go about doing this?

    Would I need to sort the arrays first? If so, its tricky because '11' would come before '2' in this case..
    Last edited by Thrillseeker; 09-14-2011 at 10:48 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Code:
    var questions = [
        ["<Question1> Question","choice1","choice2", "choice3"],
        ["<Question2> Question","choice1","choice2", "choice3"],
        ["<Question3> Question","choice1","choice2", "choice3"],
        ["<Question4> Question","choice1","choice2", "choice3"]
    ]
    var list = [];
    for (var q = 0; q < questions.length; ++q )
    {
        list[q] = questions[q][0];
    }
    document.formName.fieldName.value = list.join("\n");
    ...
    <form name="formName">
    <textarea name="fieldName"></textarea>
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Old Pedant again, I even named the quiz file after you !

    I tried the above but it still doesn't work. Here's how it stands now.


    Code:
    function linearlist()
    {
    var list = [];
    for (var l = 0; l < questions_raw.length; ++l )
    {
        list[l] = questions_raw[l][0];
    }
    document.Qtext2form.Qtext2.value = list.join("\n");
    }
    
    
    function randomOrder() { return Math.random() > 0.5 ? 1 : -1; }
    
    questions = questions_raw.sort(randomOrder);
    congrats  = congrats.sort(randomOrder);
    tch       = tch.sort(randomOrder);
    
    var qcount  = 0;
    var correct = 0;
    var okCount = 0;
    var oopsCount = 0;
    
    function nextQuestion() 
    {
        var question = questions[qcount]; 
        document.getElementById("Qtext").value = question[0]; // display the Q
        select = document.getElementById("choices");
        select.options.length = 1; // wipe out except 'choose an answer'
        for ( a = 1; a < question.length; ++a ) // take 1 answer at a time
        {
            ans = question[a];
            q = question[0];
            if ( ans.charAt(0) == "*" ) ans = ans.substring(1); // if it starts with *, get rid of the *
            if ( q.charAt(0) == ">" ) ans = ""; // if question begins with '>', displayed answer is blank
    
            select.options[a] = new Option( ans, question[a] ); // into the select box, put each of the answers (starting at a which is 1)
        } // ans=text(i.e. without the asterisk) question[a]=value
        document.getElementById("qnum").value = qcount+1;
        mode1();
        if ( q.charAt(0) == ">" ) { mode2(); } // multiple choice
       else if (q.charAt(0) == "*" ) {mode3(); } // user entry
        else { mode1()}; // standard
    
    }
    linearlist() works by pressing a button. It sorts them randomly before the list function can be run, because it has to sort randomly to display them. Even if the random order function is placed in the next question function.

    It doesn't like list[l] = questions_raw[l][0];, because it can't seem to return the l as a number (i use l instead of q because q is a global variable later on). When i replace that with a number, it makes a list of the same question repeated. So I don't get why it doesn't understand the for loop

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this code i found on internet

    var scripts = new Array();
    scripts[0] = "PHP";
    scripts[1] = "ASP";
    scripts[2] = "JavaScript";
    scripts[3] = "HTML";


    for (i=0;i<scripts.length;i++)
    {
    document.write(scripts[i] + "<br >");
    }

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    zain: That code is (a) old and (b) over-complicated.

    You can do the same thing, thus:
    Code:
    var scripts = ["PHP","ASP","JavaScript","HTML"];
    document.write( scripts.join("<br/>") );
    Don't trust everything that you find on the internet is worth reading, much less worth using.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Thrill: Doesn't make sense to me, either.

    Clearly questions_raw is okay, since you are able to sort it.

    You have the page live someplace we can look at it??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Aye, it sorts fine in a random order when I use it in the other function, but i can't seem to sort it numerically for the 'linearlist' thing.

    Its not live atm, but I'll be able to get it live this weekend then link you to it then. Cheers mate


  •  

    Posting Permissions

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