2013-04-21 8 views
0

我在學習JavaScript,所以我正在做這個項目來練習。我試圖弄清楚對象和所有工作。基本上我想要的是一個人的列表,作爲對象,並賦予每個屬性的特定屬性。然後它會問一堆問題,直到它猜出你正在想的那個人。我四處搜尋,但無法真正找到如何做到這一點。這是我到目前爲止有:使用JavaScript對象的人物猜測程序

function person(name,age,eyecolor,gender,eyeglasses) 
{ 
    this.name=name; 
    this.age=age; 
    this.eyecolor=eyecolor; 
    this.gender=gender; 
    this.eyeglasses=eyeglasses; 
} 

var Dad=new person("Dad",45,"blue","male",true); 
var Mom=new person("Mom",48,"blue","female",false); 
var Brother=new person("Brother",16,"blue","male",false); 
var Sister=new person("Sister",15,"green","female",false); 

function askQuestion(){ 

} 

function begin(){ 
    askQuestion(); 
} 

現在我要的是一個方式,我可以在askQuestion功能,選擇基於我們到目前爲止關於人知道名單的問題。然後重新計算可能的人數,然後選擇另一個問題,直到我們知道它是誰。希望我已經說清楚了。我會怎麼做?

+0

嘗試'提示(「他們的性別?」)等,並將其與人的性別進行比較。 – 2013-04-21 17:39:01

+0

好的,但我不太瞭解Javascript。什麼是提示?我如何比較呢? – eshellborn 2013-04-21 17:41:08

+2

@eshellborn SO不是代碼寫入服務。正如http://stackoverflow.com/faq所說,SO是針對「特定編程問題」的問題。 – Mooseman 2013-04-21 17:54:17

回答

4

這有點像遊戲「Guess Who?」不?好吧,這就是你要做的:

首先你爲一個人創建一個構造函數。你有這個權利。

function Person(name, age, eyecolor, gender, eyeglasses) { 
    this.name = name; 
    this.age = age; 
    this.eyecolor = eyecolor; 
    this.gender = gender; 
    this.eyeglasses = eyeglasses; 
} 

然後您創建可能的人列表。列表意味着一個數組。

var people = [ 
    new Person("Dad", 45, "blue", "male", true), 
    new Person("Mom", 48, "blue", "female", false), 
    new Person("Brother", 16, "blue", "male", false), 
    new Person("Sister", 15, "green", "female", false) 
]; 

然後你不停地提問以猜測這個人是誰。保持詢問意味着使用循環。我們將繼續循環,直到只有一個留在列表中(我們正在尋找的人)人:

while (people.length > 1) askQuestion(); 

接下來我們定義askQuestion功能。首先,我們需要選擇要問的問題。所以我們列出問題清單。這又是一個數組。我們還會存儲要測試的屬性以及truefalse條件的結果。

var questions = [ 
    ["eyecolor", "blue", "green", "Does the person have blue eyes?"], 
    ["gender", "male", "female", "Is the person a male?"], 
    ["eyeglasses", true, false, "Does the person wear eyeglasses?"] 
]; 

這三個問題都是你需要知道的,以確定誰是誰。接下來我們記錄當前正在詢問哪個問題(0,1或2)。

var question_no = 0; 

最後,我們提出的問題,以確定該人是誰:

function askQuestion() { 
    var question = questions[question_no++]; 
    var answer = confirm(question[3]) ? question[1] : question[2]; 
    var predicate = question[0]; 

    people = people.filter(function (person) { 
     return person[predicate] === answer; 
    }); 
} 

在這裏,我們詢問用戶問題,確定哪個回答他選擇和使用這些信息來篩選符合誰的人給出說明。最後,我們結束了一個人:

alert("The person you're thinking about is " + people[0].name + "."); 

見工作演示在這裏:http://jsfiddle.net/9g6XU/

+0

哇!非常感謝!完美的作品! – eshellborn 2013-04-21 18:24:29

+0

現在,快點,我們只是說'爸爸'有棕色的眼睛。我們可以爲此做些什麼?會不會有一個快速的方法來改變它,以便工作? – eshellborn 2013-04-21 18:36:49

1

這是我會怎麼做。它比Aadit的答案短,在我看來,更簡單,更易於理解。

列出人員。只要你想

var questions = { 
    "Are they male or female?" : 'gender', 
    "What is their eye color?" : 'eyecolor', 
    "Do they wear glasses?" : 'eyeglasses' 
}; 

這可能與儘可能多的性能進行擴展:使用文字的數組:

var people = [Dad, Mom, Brother, Sister]; 

我喜歡組織我的代碼,所以我會提出的問題中的對象。

然後:

for (question in questions) { //This is how you loop through an object 
    var property = questions[question]; //This gets the second part of the object property, e.g. 'gender' 
    var answer = prompt(question); 

    //filter is an array method that removes items from the array when the function returns false. 
    //Object properties can be referenced with square brackets rather than periods. This means that it can work when the property name (such as 'gender') is saved as a string. 

    people = people.filter(function(person) { return person[property] == answer }); 

    if (people.length == 1) { 
     alert("The person you are thinking of is " + people[0].name); 
     break; 
    } 
    if (people.length == 0) { 
     alert("There are no more people in the list :("); 
     break; 
    } 
} 

而且我也一樣,使你成爲小提琴。 Here it is.