PDA

View Full Version : Inserting Images into Arrays



tfortwit
Dec 6th, 2004, 01:14 AM
Hi

I am working on an online quiz (for college). I have to use different types of questions, including images as the answers...How do I substitute images for the question?

The code I have at the moment is as below ( It is in progress hense the silly questions and answers... :D ), I am VERY new to computing and would appreciate a simple explanation...Also, how can I stop the questions appearing randomly?

<center>
<HTML>
<HEAD>
<TITLE>Rachael's Disney Quiz Quiz</TITLE>

<SCRIPT LANGUAGE="JavaScript"> document.write
var questionNumber;
function answerCorrect(questionNumber, answer)
{
// declare a variable to hold return value
var correct = false;

// if answer provided is same as correct answer then correct variable is true
if (answer == answers[questionNumber])
correct = true;

// return whether the answer was correct (true or false)
return correct;
}

function getQuestion()
{
questionNumber = Math.floor(Math.random() * (questions.length));
var questionHTML = "<P>" + questions[questionNumber][0] + "</P>";
var questionLength = questions[questionNumber].length;
var questionChoice;
for (questionChoice = 1;questionChoice < questionLength;questionChoice++)
{
questionHTML = questionHTML + "<INPUT TYPE=radio NAME=radQuestionChoice"
if (questionChoice == 1)
{

questionHTML = questionHTML + " CHECKED";
}
questionHTML = questionHTML + ">";
questionHTML = questionHTML + questions[questionNumber][questionChoice];
questionHTML = questionHTML + "<BR>";
}
document.QuestionForm.txtQNumber.value = questionNumber + 1;
return questionHTML;
}
function buttonCheckQ_onclick()
{
var answer = 0;
while (document.QuestionForm.radQuestionChoice[answer].checked != true)
{
answer++;
}
answer = String.fromCharCode(65 + answer);
if (answerCorrect(questionNumber,answer) == true)
{
alert("CONGRATULATIONS ... You answered correctly");
}
else
{
alert("DUH!! - You answered wrong");
}
window.location.reload();
}


</SCRIPT>


</HEAD>



<BODY>
<SCRIPT LANGUAGE=JavaScript>

// questions and answers arrays will holds questions and answers
var questions = new Array();
var answers = new Array();
// define question 1 </h1>

questions[0] = new Array();

// the question
questions[0][0] = "Mr Disney's First name was?";

// first choice
questions[0][1] = "Walt";

// second choice
questions[0][2] = "Micky";

// third choice
questions[0][3] = "Pluto";
// assign answer for question 1
answers[0] = "A";

// define question 2
questions[1] = new Array();
questions[1][0] = "Winnie The Pooh is a:";
questions[1][1] = "Tiger";
questions[1][2] = "Bear";
questions[1][3] = "Donkey";
questions[1][4] = "Pig";

// assign answer for question 2
answers[1] = "B";

// define question 3
questions[2] = new Array();
questions[2][0] = "Which of these appeared in The Little Mermaid";
questions[2][1] = "Ariel"
questions[2][2] = "Donald";
questions[2][3] = "John Brown";
questions[2][4] = "Tinkerbell";

// assign answer for question 3
answers[2] = "A";


// define question 4
questions[3] = new Array();
questions[3][0] = "Walt Disney was:";
questions[3][1] = "Welsh"
questions[3][2] = "English";
questions[3][3] = "Japanese";
questions[3][4] = "American";

// assign answer for question 4
answers[3] = "D";


// define question 5
questions[4] = new Array();
questions[4][0] = "Ursuala is a:";
questions[4][1] = "Beautiful Mermmaid"
questions[4][2] = "A signing crab";
questions[4][3] = "An ugly octopus";
questions[4][4] = "A Scardy Shark";

// assign answer for question 4
answers[4] = "C";

// define question 6
questions[5] = new Array();
questions[5][0] = "Who is this?";
questions[5][1] =
questions[5][2] = "A signing crab";
questions[5][3] = "An ugly octopus";
questions[5][4] = "A Scardy Shark";

// assign answer for question 4
answers[5] = "C";

</SCRIPT>
<FORM NAME="QuestionForm">
Question
<INPUT TYPE="text" NAME=txtQNumber SIZE=1>
<SCRIPT LANGUAGE=JavaScript>
document.write(getQuestion());
</SCRIPT>
<INPUT TYPE="button" VALUE="Are you correct" NAME=buttonCheckQ
onclick="return buttonCheckQ_onclick()">
</FORM>
</BODY>
</HTML>


</SCRIPT>


<img src="C:\Documents and Settings\Mr Phillips\My Documents\Coursework\Mickeyearthears.gif" ></i></u>
<HTML>
<HEAD>
<TITLE>Rachael's Disney Quiz</TITLE>

<SCRIPT LANGUAGE="JavaScript">document.bgColor ="pink"; document.write
var questionNumber;
function answerCorrect(questionNumber, answer)
{
// declare a variable to hold return value
var correct = false;

// if answer provided is same as correct answer then correct variable is true
if (answer == answers[questionNumber])
correct = true;

// return whether the answer was correct (true or false)
return correct;
}

function getQuestion()
{
questionNumber = Math.floor(Math.random() * (questions.length));
var questionHTML = "<P>" + questions[questionNumber][0] + "</P>";
var questionLength = questions[questionNumber].length;
var questionChoice;
for (questionChoice = 1;questionChoice < questionLength;questionChoice++)
{
questionHTML = questionHTML + "<INPUT TYPE=radio NAME=radQuestionChoice"
if (questionChoice == 1)
{

questionHTML = questionHTML + " CHECKED";
}
questionHTML = questionHTML + ">";
questionHTML = questionHTML + questions[questionNumber][questionChoice];
questionHTML = questionHTML + "<BR>";
}
document.QuestionForm.txtQNumber.value = questionNumber + 1;
return questionHTML;
}
function buttonCheckQ_onclick()
{
var answer = 0;
while (document.QuestionForm.radQuestionChoice[answer].checked != true)
{
answer++;
}
answer = String.fromCharCode(65 + answer);
if (answerCorrect(questionNumber,answer) == true)
{
alert("CONGRATULATIONS...You answered correctly");
}
else
{
alert("DUH...YOU answered wrong");
}
window.location.reload();
}


</SCRIPT>


</HEAD>


THANK YOU :thumbsup:

hemebond
Dec 6th, 2004, 01:50 AM
What on Earth? Do you have 3 head sections there? Markup your post properly.

http://codingforums.com/misc.php?do=bbcode

Jalenack
Dec 6th, 2004, 03:09 AM
Welcome to CodingForums!

help us help you:

put your code in[CODE] tags (check the link from Hemebond). It is very hard to read your code how it is now.
And post this in the correct forum...this should be a question for the Javascript forum...They're experts over there.
Good luck!

hemebond
Dec 6th, 2004, 05:36 AM
Ask the moderator to move it for you. Don't double post.

tfortwit
Dec 7th, 2004, 12:53 AM
Thanks both...I will re-look at the code and post in correct forum after I have sorted it out, sorry to have been any trouble...and thanks for taking the time to look at my question.

Regards
Alison