簡體   English   中英

多項選擇/修正程序 (JavaScript)

[英]Multiple-Choice / Correction program (JavaScript)

(編碼JS)

我正在嘗試編寫一個更正程序,應用於某種多項選擇測驗 - 有 3 個可能的答案(A、B、C)

我顯然需要變量(名稱)

var Simon = [A, B, C, A, B, C, A, B, C]
var (new name) = [.., ..., ..., .. and so on]

以及某種正確的字符串: [A, B, B, B, C, A , B, C]

現在,如果我例如

console.log(functionName([A, B, B, A, B, C, C, A, B])

我希望輸出是:“西蒙”,因為他有最正確的答案(5 對),該函數必須檢查並匹配每個名稱/變量的索引。

我不久前開始編碼,所以我有點新(菜鳥)。 也許我已經面臨這種挑戰了...

提前致謝!

您可以編寫一個函數,為數組中的每個元素執行該函數(fe Simon)。 然后,此函數應該將該數組的當前元素 (Simon) 與正確答案進行比較。 這可能是這樣的:

var simon = ["A", "C", "B", "D"];
var correctAnswers = ["A", "B", "C", "D"];
var simonsScore = 0;

// This executes the fuction checkCorrectAnswers for each element in Simon
simon.forEach(checkCorrectAnswers);

alert("Correct Answers: " + simonsScore);

// item is the current item and index is its index
function checkCorrectAnswers(item, index) {
  // Compare the answer with the correct answer
  if(item == correctAnswers[index]) {
        // if they are correct, increase simonsScore
        simonsScore++;
  }

}

編輯:

好的,所以如果你“只”需要學生的名字,那么你需要一個二維數組。 第一級數組是所有學生的“列表”。 第二個層次是學生和他們的“數據”。 數據是他們的名字、答案和分數。 所以你可以像我們上面做的一樣(使用 forEach 遍歷數組),但你需要在兩個級別上都這樣做。

下面的代碼記錄了分數最高的學生姓名和他們的分數。

這對你有用嗎?

var answers = [["Simon", "A", "C", "B", "D"], ["Bob", "A", "B", "E", "D"]];
var correctAnswers = ["correctAnswers", "A", "B", "C", "D"];
var highestScore = 0;
var highestScoreName = "";

// This executes the fuction checkCorrectAnswers for each element in answers
answers.forEach(compareStudents);
// Log your result
console.log("Most Answers are " + highestScore + " by " + highestScoreName);

// Function ---------------------------------------------------

function compareStudents (item, index, arr) {
// First we create a score variable for each student an push it in their array
  var score = 0;
  arr[index].push(score);

    // Now we check answers for each Student
    arr[index].forEach(checkCorrectAnswers);
 
 // Now their score has been updated in checkCorrectAnswers,
 // so now we can check if their score is higher than the highest score
 // to get the last element of the array thats the current student
  var score = arr[index][arr[index].length-1];

    // and compare it with the highscore
  if(score > highestScore) {
    highestScore = score;
    highestScoreName =  arr[index][0]; // thats where we find the name of the student
  }
  
}

// item is the current item and index is its index, arr is the array we're checking
function checkCorrectAnswers(item, index, arr) {
  // get the current score from the array with is the last element and save it
  var score = arr[arr.length-1];
  arr.pop();
  
  // compare the answwer with the correct one
  // (the first element compared will be their name compared with "correctAnswers", which will never be true)
  if(item == correctAnswers[index]) {       
        score++;
      console.log("Correct answer: " +item + " = " + correctAnswers[index] + ". Total of " + score + " correct answers.");
  }
  
  // Push the score into the array, so the last element is always the score
  arr.push(score);
  
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM