人们用JavaScript对象猜程序
People guesser program with JavaScript objects
我正在尝试学习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
函数。首先我们需要选择问什么问题。所以我们列了一个问题清单。这也是一个数组。我们还将存储要测试的属性以及true
和false
条件下的结果。
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;
}
}
我也给你做了一把小提琴。
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- Windows 8 Javascript应用程序XML对象
- getSript在同一对象上多次添加处理程序
- 在这个使用hasOwnProperty的对象扩展程序中有一个错误,I'我不确定那个bug是什么,也不确定这个扩展
- jsplumb中的Click事件处理程序丢失“;这个“;对象
- 读取在具有IE的工作程序中生成的对象URL
- JavaScript1.7区分数组和对象的迭代程序
- 为什么Javascript程序员更喜欢在代码中重复字符串作为常量,而不是使用枚举类型的对象
- 扩展引导程序的typeahead以便获取对象而不是字符串
- 向调用全局javascript函数的对象添加处理程序
- 动态添加对象的jQuery事件处理程序
- 在requirejs+提线木偶应用程序中传递应用程序对象
- 使用javascript访问经典的asp应用程序对象
- Firefox's在其他浏览器中的应用程序对象设备
- 如何使用“严格使用”;不修改我的实用程序对象的作用域
- 调用同一模板帮助程序对象中的另一个属性
- asp.net mvc 3-我可以从“应用程序”对象存储和读取数据吗?从javascript访问它
- 每当测试导航到另一个页面时,我是否必须创建一个新的web驱动程序对象
- 基于Flash的录音机/播放器应用程序对象如何与网页中的JavaScript进行通信?