2016-10-01 62 views
-1

只是一個簡單的JavaScript測驗。下面的代碼工作,但我有幾個關於它的問題。這個數組是如何工作的?

var player = prompt("Hello, welcome to the quiz, what is your name?"); 
var score = 0; 

var questions = [ 
    ["What is my name?", "Joe"], 
    ["What is my age?", 27], 
    ["What is my favourite sport?", "Football"], 
    ["What is my job?", "Web Developer"], 
    ["What is my eye color?", "Blue"] 
]; 

function askQuestions(question) { 
var answer = prompt(question[0],''); 
if(answer === question[1]) { 
    alert('Correct!') 
} else { 
    alert('Wrong!'); 
} 
}; 

for(var i= 0; i < questions.length; i++) { 
    askQuestions(questions[i]); 
}; 

所以我不明白的第一件事是在askQuestions函數中提示符有'questions [0]'。我知道我們有一個循環,但這不是循環的一部分嗎?所以肯定這個變量只是將第一個數組存儲在questions變量中。

第二個參數是一個空字符串''。這只是存儲答案嗎?這是如何運作的?

我明白休息只是有問題的功能。

如果有人能解釋這將是偉大的!

乾杯

+1

問題[0]和問題[1]中函數不引用數組,只引用數組中單個對象的第一個和第二個屬性。 ([0]將成爲問題,[1]答案) –

+1

對象數組本來是一個更好的選擇,即一個包含兩個元素數組的數組 – Bergi

回答

1

var questions是2D陣列的陣列,並且每個這些2D陣列的在循環到askQuestion通過。然後,例如,當您在第一個循環中傳遞question [0]以詢問Question時,您確實傳遞了對象['我的名字是什麼','Joe']

函數內部函數askQuestion然後參數數組的第一個元素(所以,'我的名字是什麼'),並將答案與參數數組的第二個元素進行比較(所以,'喬')

也許,如果你寫這樣askQuestion,你會明白更多工作流程:

function askQuestions(**paramArray**) { 
    var answer = prompt(**paramArray**[0],''); 
    if(answer === **paramArray**[1]) { 
    alert('Correct!') 
    } else { 
    alert('Wrong!'); 
    } 
}; 

關於提示,第二個參數它只是默認顯示的文本。

+0

謝謝。每個問題和答案如何總是[0]和[1]?我在想,因爲有5個數組,第一個數組將是[0],下一個[1],[2]等等...... 還默認文本?你是什​​麼意思? 謝謝! –

+0

就是這樣,因爲問題(複數)是一個組合數組,問題var的第一個元素將是一個帶有[「我的名字是什麼?」,「Joe」]的二維數組,第二個問題的元素是[「什麼是我的年齡嗎?「,27]等等......當它作爲參數傳遞時,askQuestions只是看到一個二維數組(每個問題一般var的元素)並使用它。 – DvTr

1

因此,最好的方法來看看發生了什麼,只需把變量,並插入自己。

askQuestions(問題[])

插頭在I:

askQuestions(問題[0]

塞問題[0]

askQuestions ( [「我的名字是什麼?」,「喬」]

插件功能

function askQuestions(["What is my name?", "Joe"]) { 
var answer = prompt("What is my name?",''); 
if(answer === "Joe") { 
    alert('Correct!') 
} else { 
    alert('Wrong!'); 
} 
}; 

我評論這裏的代碼:

//player inputs their name and it is stored in var player 
var player = prompt("Hello, welcome to the quiz, what is your name?"); 
//set var score to 0; 
var score = 0; 

//questions is an array, of arrays. so questions[0] is ["What is my name?", "Joe"] and questions[0][0] is "What is my name?" 
var questions = [ 
["What is my name?", "Joe"], 
["What is my age?", 27], 
["What is my favourite sport?", "Football"], 
["What is my job?", "Web Developer"], 
["What is my eye color?", "Blue"] 
]; 

//askQuestions is a function that will be called in the for loop below 
function askQuestions(question) { 
//set var answer to whatever the user types in, pop up will say whatever question[0] is Or in this case question[i][0] because we pass in question[i] 
var answer = prompt(question[0],''); 
//here we just check if what they type in is the same as the answer we expect 
if(answer === question[1]) { 
alert('Correct!') 
} else { 
    alert('Wrong!'); 
} 
}; 

//this loop is going to to run for the questions.length, in this case 5 times, and each time it will call askQuestions(question[0]) then askQuestions(question[1]) etc 
for(var i= 0; i < questions.length; i++) { 
    askQuestions(questions[i]); 
}; 

希望幫助... :)