2016-03-26 7 views
0

選択の順序をランダム化したい。私は選択肢の順序をシャッフルするはずのスクリプトを追加しましたが、それはできませんでした。クイズをデバッグすると何も表示されません。Javascriptクイズ - Array.prototypeを使用してランダムな順序で選択肢を表示する

ここに私が追加されたコードです:

function shuffle(array) { 
    var currentIndex = array.length, temporaryValue, randomIndex; 

    // While there remain elements to shuffle... 
    while (0 !== currentIndex) { 

    // Pick a remaining element... 
    randomIndex = Math.floor(Math.random() * currentIndex); 
    currentIndex -= 1; 

    // And swap it with the current element. 
    temporaryValue = array[currentIndex]; 
    array[currentIndex] = array[randomIndex]; 
    array[randomIndex] = temporaryValue; 
    } 

    return array; 
} 

Javascriptのクイズ:

var quiz = [{ 
      "question": "What is the full form of IP?", 
      "choices": ["Internet Provider", "Internet Port", "Internet Protocol" , "Other"], 
      "correct": "Other" 

     }, { 
      "question": "Who is the founder of Microsoft?", 
      "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak" , "Martin Shaba"], 
      "correct": "Bill Gates" 
     }, { 
      "question": "What was your first dream?", 
      "choices": ["8 bits", "64 bits", "1024 bits"], 
      "correct": "8 bits" 
     }, { 
      "question": "The C programming language was developed by?", 
      "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"], 
      "correct": "Dennis Ritchie" 
     }, { 
      "question": "What does CC mean in emails?", 
      "choices": ["Carbon Copy", "Creative Commons", "other"], 
      "correct": "Carbon Copy" 
     }]; 
+1

可能な重複[ランダム化する方法(シャッフル) JavaScript配列?](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) –

答えて

1

Array.prototypescramble機能を追加します。

if (!("scramble" in Array.prototype)) { 
 
    Object.defineProperty(Array.prototype, "scramble", { 
 
    enumerable: false, 
 
    value: function() { 
 
     var o, i, ln = this.length; 
 
     while (ln--) { 
 
     i = Math.random() * (ln + 1) | 0; 
 
     o = this[ln]; 
 
     this[ln] = this[i]; 
 
     this[i] = o; 
 
     } 
 
     return this; 
 
    } 
 
    }); 
 
} 
 
var quiz = [{ 
 
    "question": "What is the full form of IP?", 
 
    "choices": ["Internet Provider", "Internet Port", "Internet Protocol", "Other"], 
 
    "correct": "Other" 
 
}]; 
 

 
quiz.forEach(q => q.choices.scramble()); 
 

 
console.log(quiz[0].choices);

は、

もともと私が提案した:

quiz.forEach(q => q.choices.sort(() => Math.random() - .5)); 

DanDavisは、特定の方法は、合理的な配分を達成しなかったことを指摘しました。

+0

あなたの答えをありがとう!これは動作しますが、選択肢ではなく質問をランダム化しています。@canon – Martin

+1

完璧!どうもありがとうございます! @canon – Martin

+2

そのようなシンプルで驚くべき答えとその1つのライナーだから、それはボーナス、よく行われたキャノンです。 –

1

あなたは「その他」あなたはこのコードでそれを達成でき、シャッフルした後、最後の選択肢として残っているしたい場合は、次の

var quiz = [{ 
 
    "question": "What is the full form of IP?", 
 
    "choices": ["Internet Provider", "Internet Port", "Internet Protocol", "Other"], 
 
    "correct": "Other" 
 
}, { 
 
    "question": "Who is the founder of Microsoft?", 
 
    "choices": ["Bill Gates", "Steve Jobs", "Steve Wozniak", "Martin Shaba"], 
 
    "correct": "Bill Gates" 
 
}, { 
 
    "question": "What was your first dream?", 
 
    "choices": ["8 bits", "64 bits", "1024 bits"], 
 
    "correct": "8 bits" 
 
}, { 
 
    "question": "The C programming language was developed by?", 
 
    "choices": ["Brendan Eich", "Dennis Ritchie", "Guido van Rossum"], 
 
    "correct": "Dennis Ritchie" 
 
}, { 
 
    "question": "What does CC mean in emails?", 
 
    "choices": ["Carbon Copy", "Creative Commons", "Other"], 
 
    "correct": "Carbon Copy" 
 
}]; 
 

 
function shuffle(array) { 
 
    var temporaryValue, randomIndex; 
 
    var currentIndex = array.length; 
 

 
    // While there remain elements to shuffle... 
 
    while (currentIndex > 1) { 
 

 
     // Pick a remaining element... 
 
     randomIndex = Math.floor(Math.random() * currentIndex); 
 
     currentIndex -= 1; 
 

 
     // And swap it with the current element. 
 
     temporaryValue = array[currentIndex]; 
 
     array[currentIndex] = array[randomIndex]; 
 
     array[randomIndex] = temporaryValue; 
 
    } 
 

 
    return array; 
 
} 
 

 
quiz.forEach(function(question) { 
 
    var otherIndex; 
 
    var choices = question.choices; 
 
    var lastIndex = choices.length - 1; 
 
    shuffle(choices); 
 
    otherIndex = choices.indexOf('Other'); 
 
    if (otherIndex >= 0) { 
 
     choices[otherIndex] = choices[lastIndex]; 
 
     choices[lastIndex] = 'Other'; 
 
    } 
 
    console.log(choices); 
 
});

+1

ええ、それは配列をランダム化しません...そして、なぜそれを探して真ん中で配列を突然変異させるのではなく、最後のものを保存してpush()しないのですか? – dandavis

+1

良い編集、私の前のコメントを無視してください。 – dandavis

関連する問題