2013-04-21 34 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.