人们用JavaScript对象猜程序

People guesser program with JavaScript objects

本文关键字:程序 对象 JavaScript      更新时间:2023-09-26

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

这有点像游戏"猜猜谁?"不是吗?这就是你要做的:

首先创建一个person的构造函数。你说对了。

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

然后你创建一个可能的人的列表。list是指数组。

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/

我是这样做的。它比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;
    }
}

我也给你做了一把小提琴。