0

我正在尝试学习 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函数中根据我们迄今为止对该人的了解从列表中选择一个问题。然后重新计算可能是谁,然后选择另一个问题来问,直到我们知道它是谁。希望我已经说清楚了。我该怎么做?

4

2 回答 2

4

这有点像游戏“猜猜谁? ”不是吗?好的,这就是你要做的:

首先,您为一个人创建一个构造函数。你说对了。

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/

于 2013-04-21T18:05:25.830 回答
1

这就是我将如何做到的。它比 Aadit 的答案要短,而且在我看来,它更简单、更容易理解。

列出人员名单。使用数组文字:

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

我喜欢结构化我的代码,所以我会将问题放在一个对象中:

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

可以根据需要使用任意数量的属性对其进行扩展。

然后:

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;
    }
}

我也给你做了个小提琴。这里是。

于 2013-04-21T18:24:55.000 回答