need help

O

OzThor

OK, i'm only just starting to understand javascript.... and trying to get
the following code to work....

I have a program that generates a webpage, which is were i got the code
from. trouble is that the original code uses frams that i do not want so
have cut it back to the following...

It now gives the opening screen, but when you click on "Check" it dose not
update the score etc...

could anyone help here, pointing out why and how to fix???
i'd like to learn as i get this working....

thanks....

the code......
a copy is attached..


<html>
<head>
<title>
Mix & Match
</title>

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



x = new Array();




var CorrectIndicator = ':)';
var IncorrectIndicator = 'X';
var YourScoreIs = 'Your score is '; //'Your score is: ';
var CorrectResponse = 'Correct! Well done.';
var IncorrectResponse = 'Sorry! Try again. Incorrect matches have been
removed.';
var TotalUnfixedLeftItems = 0;
var TotCorrectChoices = 0;
var Penalties = 0;
var ExerciseTitle = 'EMC Mix & Match'; //'Test exercise';
var ExerciseSubtitle = 'Matching exercise'; //'Multiple-choice';
var Instructions = 'Match the items on the right with the items on the
left.'; //'';
var DefaultResponse = '[strDefaultResponse]';
var ReadingURL = '';








var NavBar='<br>';



var TitleCode = '<tr><td align="center" valign="middle" bgcolor="#ffffff">';
TitleCode += '<font size="+1" face="Geneva,Arial" color="#000033">' +
ExerciseTitle + '</font><br />';
TitleCode += '<font size="-1" face="Geneva,Arial" color="#000033">' +
ExerciseSubtitle + '<br />';
TitleCode += '</font></td></tr>';

var TableOpener = '<center><form name="QForm">';
TableOpener += '<table border="0">';

var IRow = '<tr><td valign="top" bgcolor="#bbbbee">';
IRow += '<font face="Geneva,Arial"
color="#000000">[strLeftItem]&nbsp;&nbsp;&nbsp;&nbsp;</font></td>';
IRow += '<td valign="top" bgcolor="#bbbbee">';
IRow += '<font face="Geneva,Arial"
color="#000000">[strRightItem]</font></td>';
IRow += '<td valign="top" bgcolor="#bbbbee"><font face="Geneva,Arial"
color="#000000">';
IRow += '&nbsp;&nbsp;&nbsp;[Mark]';
IRow += '</font></td></tr>';

var CheckButton = '<tr><td valign="top" align="center" bgcolor="#bbbbee"
colspan="3"><br /><input type="button" value="Check"
OnClick="CheckAnswers()"></input></td></tr>';

TableCloser = '</table></form></center>';

var DropDownList = '';
var Exercise = '';








function Shuffle(InArray){
Temp = new Array();
var Len = InArray.length;

var j = Len;

for (var i=0; i<Len; i++){
Temp = InArray;
}

for (i=0; i<Len; i++){
Num = Math.floor(j * Math.random());
InArray = Temp[Num];

for (var k=Num; k < j; k++) {
Temp[k] = Temp[k+1];
}
j--;
}
return InArray;
}

function WriteFeedback(Feedback) {
//Build the output string
var OutString = '';
// OutString += '<html>';
// OutString += '<body background="" bgcolor="#ffffff" text="#000000"
link="#0000ff" vlink="#0000cc">';
OutString += '<center><table border="0" cellpadding="5" cellspacing="1"
width="85%">';
OutString += NavBar;
if (Feedback.length>0){
OutString += '<tr><td bgcolor="#bbbbee" align="center">';
OutString += '<font face="Geneva,Arial" size="-1" color="#000000">';
OutString += Feedback;
OutString += '</font></td></tr>'
}
OutString += '</table></center>';
// OutString += '</body></html>';



//Write it to the frame

document.write(OutString);

}

function BuildSelector(){
DropDownList = '<select name="sel[INum]">';
DropDownList += '<option value="??? ">??? </option>';
for (var i=0; i<RItems.length; i++){
DropDownList += '<option value="' + EscapeDoubleQuotes(RItems[0]) +
'">' + RItems[0] + '</option>';
}
DropDownList += '</select>';
}

function StartUp(){


CorrectIndicator = '<nobr>' + CorrectIndicator + '</nobr>';
IncorrectIndicator = '<nobr>' + IncorrectIndicator + '</nobr>';

//Write the top frame
WriteFeedback(Instructions);

//Create the right-item/distractor array, and shuffle it
var DuplicateItem = false;
for (var i=0; i<I.length; i++){
//If it's not fixed
if (I[2] < 1){
if (I[0].length > 0){
TotalUnfixedLeftItems++;
}
DuplicateItem = false;
//and it's not already in the array
for (var j=0; j<RItems.length; j++){
if (I[1] == RItems[j][0]){
DuplicateItem = true;
RItems[j][1][RItems[j][1].length] = i;
}
}
//add it to the array
if (DuplicateItem == false){
RItems[RItems.length] = new Array();
RItems[RItems.length-1][0] = I[1];
RItems[RItems.length-1][1] = new Array();
RItems[RItems.length-1][1][0] = i;
}
}
}
RItems = Shuffle(RItems);

//Now tell the I array items which of the selector items matches to it
for (i=0; i<RItems.length; i++){
for (j=0; j<RItems[1].length; j++){
I[RItems[1][j]][3] = i;
}
}

//Now use the RItems array to build the drop-down list selector
BuildSelector();

//Create arrays
CreateStatusArrays();



//Build and show the exercise
BuildExercise();
DisplayExercise(Exercise);


}


function BuildExercise(){
var TempRow = '';
Exercise = '';
for (var i=0; i<I.length; i++){
//if there's a left item -- ie it's not a distractor
if (I[0].length > 0){
TempRow = IRow;
TempRow = ReplaceStuff('[strLeftItem]', I[0], TempRow);
//if not fixed and not yet answered correctly
if ((I[2] < 1)&&(Status[0] < 1)){
TempRow = ReplaceStuff('[strRightItem]', DropDownList, TempRow);
}
else{
TempRow = ReplaceStuff('[strRightItem]', I[1], TempRow);
}
//if it's not fixed, and it's been attempted, mark it appropriately
if ((I[2] < 1)&&(Status[1] > 0)){
if (Status[0] < 1){
TempRow = ReplaceStuff('[Mark]', IncorrectIndicator, TempRow);
}
else{
TempRow = ReplaceStuff('[Mark]', CorrectIndicator, TempRow);
}
}
else{
TempRow = ReplaceStuff('[Mark]', '', TempRow);
}
TempRow = ReplaceStuff('[INum]', i, TempRow);
Exercise += TempRow;
}
}
//Add the check button
Exercise += CheckButton;

//Make it into a table
Exercise = TableOpener + Exercise + TableCloser;
}

I = new Array();
I[0] = new Array();
I[0][0] = 'Elvis';
I[0][1] = 'love me tender';
I[0][2] = 0;
I[1] = new Array();
I[1][0] = 'queen';
I[1][1] = 'help';
I[1][2] = 0;
I[2] = new Array();
I[2][0] = 'beatles';
I[2][1] = 'yesterday';
I[2][2] = 0;
I[3] = new Array();
I[3][0] = 'acdc';
I[3][1] = 'ego';
I[3][2] = 0;
I[4] = new Array();
I[4][0] = 'and one more';
I[4][1] = 'any song';
I[4][2] = 0;


Status = new Array();

RItems = new Array();

function CreateStatusArrays(){
for (var x=0; x<I.length; x++){
Status[x] = new Array();
Status[x][0] = 0; // Item not matched correctly yet
Status[x][1] = 0; //Tries at this item so far
}
}

function ReplaceStuff(Token, Replacement, InString){
var i = InString.indexOf(Token);
var FirstBit = '';
var LastBit = '';
while (i>-1){
FirstBit = InString.substring(0, i);
LastBit = InString.substring(i + Token.length, InString.length);
InString = FirstBit + Replacement + LastBit;
i = InString.indexOf(Token);
}
return InString;
}

function EscapeDoubleQuotes(InString){
var Result = '';
for (var i=0; i<InString.length; i++){
if (InString.charAt(i) == '"'){
Result += '&quot;';
}
else{
Result += InString.charAt(i);
}
}
return Result;
}

function DisplayExercise(StuffToDisplay){
//Build the output string
var OutString = '';
// OutString += '<html>';
// OutString += '<body background="" bgcolor="#ffffff" text="#000000"
link="#0000ff" vlink="#0000cc">';
OutString += '<center><table border="0" cellpadding="5" cellspacing="1"
width="85%">';
OutString += TitleCode;
OutString += '<tr><td bgcolor="#bbbbee" align="center">';
OutString += '<font face="Geneva,Arial" size="-1">';
OutString += StuffToDisplay;
OutString += '</font></td></tr>';
OutString += NavBar;
OutString += '</table></center>';
// OutString += '</body></html>';


//Write it to the frame

document.write(OutString);

}

function GetAnswer(INum){
var Result = -1;
var s = eval('document.QForm.sel' + INum);
if (s != null){
Result = s.selectedIndex - 1;
}
return Result;
}

function CheckAnswers(){
var AllDone = true;
TotCorrectChoices = 0;
//for each item not fixed or a distractor
for (var i=0; i<I.length; i++){
if ((I[2] < 1)&&(I[0].length > 0)){
//if it hasn't been answered correctly yet
if (Status[0] < 1){
//Add one to the number of tries for this item
Status[1]++;
//Get the answer
if (GetAnswer(i) == I[3]){
//The answer is correct, so set the status flag
Status[0] = 1;
}
//else the answer is wrong, so remember that
else{
AllDone = false;
}
}
//If it's correct, count it
if (Status[0] == 1){
TotCorrectChoices++;
}
}
}
//Calculate the score
var Score =
Math.floor(((TotCorrectChoices-Penalties)/TotalUnfixedLeftItems)*100);
var Feedback = '';

//Build the feedback
if (AllDone == true){
Feedback = CorrectResponse + '<br />' + YourScoreIs + Score + '%.';
}
else{
Feedback = IncorrectResponse + '<br />' + YourScoreIs + Score + '%.';
//Penalty for incorrect check
Penalties++;
}
//Show the feedback and rebuild the exercise
WriteFeedback(Feedback);



BuildExercise();
DisplayExercise(Exercise);
}



</script>


</head>
<body onload="StartUp()">


// <script>
// StartUp()
// </script>


</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top