...

View Full Version : Displaying all the arrays in a variable



Thrillseeker
09-14-2011, 10:33 PM
Hey,

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


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..

Old Pedant
09-14-2011, 11:54 PM
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>
...

Thrillseeker
09-15-2011, 11:27 AM
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.



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

zain654321
09-15-2011, 12:25 PM
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 >");
}

Old Pedant
09-15-2011, 04:53 PM
zain: That code is (a) old and (b) over-complicated.

You can do the same thing, thus:


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.

Old Pedant
09-15-2011, 04:57 PM
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??

Thrillseeker
09-16-2011, 10:24 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum