2014-04-22 62 views
0

我需要添加多個問卷,使用相同的代碼/陣列。我怎樣才能做到這一點?需要多個答案添加到單個陣列

// Insert answers to questions 
answers[0] = ["Apple", "Test"] ; 
answers[1] = "Dynamic HTML"; 
answers[2] = "Netscape"; 
answers[3] = "Common Gateway Interface"; 

正如你可以看到我剛剛嘗試添加我的其他回答到代碼陣列不同的測驗,但不承認的答案(測試)。這樣做的最好方法是什麼? (有一個「或」命令或類似的東西

這裏是我的整個代碼:

// Insert number of questions 
var numQues = 4; 

// Insert number of choices in each question 
var numChoi = 3; 

// Insert number of questions displayed in answer area 
var answers = new Array(4); 


// Insert answers to questions 
answers[0] = ["Apple", "Test"] ; 
answers[1] = "Dynamic HTML", "Test"; 
answers[2] = "Netscape", "Test"; 
answers[3] = "Common Gateway Interface", "Test"; 

// Do not change anything below here ... 
function getScore(form) { 
var score = 0; 
var currElt; 
var currSelection; 
for (i=0; i<numQues; i++) { 
currElt = i*numChoi; 
for (j=0; j<numChoi; j++) { 
    currSelection = form.elements[currElt + j]; 
    if (currSelection.checked) { 
    if (currSelection.value == answers[i]) { 
     score++; 
     break; 
    } 
    } 
} 
} 
score = Math.round(score/numQues*100); 
form.percentage.value = score + "%"; 
var correctAnswers = ""; 
for (i=1; i<=numQues; i++) { 
correctAnswers += i + ". " + answers[i-1] + "\r\n"; 
} 
form.solutions.value = correctAnswers; 
} 
// End --> 

所以這只是爲了更容易理解。我想有幾個不同的測驗的鏈接這個代碼,用來答案,而不是重複整個代碼每次測驗的。需要什麼樣的修改做?

+0

我你說_just以後的事情失去使其更容易understand_ –

+0

我有各種HTML網頁測驗 - 所有這些目前鏈接到此代碼。只有答案蘋果,動態HTML,Netscape或通用網關接口一號測驗工作的那一刻 – user3560464

+0

因爲我將所有的答案,測試第二測驗,但它不工作 – user3560464

回答

0

如果你想你的答案數組是一個數組的數組,你應該有[周圍1線] ,2和3?

就像這個...

answers[0] = ["Apple", "Test"] ; 
answers[1] = ["Dynamic HTML", "Test"]; 
answers[2] = ["Netscape", "Test"]; 
answers[3] = ["Common Gateway Interface", "Test"]; 

如果我可以在該行下面編輯,要求我不要這樣做可能會更容易。我覺得你真的想要的東西像

currSelection.value == answers[i][j] 

,而不是

currSelection.value == answers[i]