问题描述
我正在编写一本简单的地址簿。这是我的代码:
var bob = {
firstName:Bob,
lastName: Jones,
phoneNumber:(650)777-7777,
电子邮件:[email protected]
} ;
var mary = {
firstName:Mary,
lastName:Johnson,
phoneNumber:(650)888-8888,
电子邮件:[email protected]
};
var contacts = [bob,mary];
function printPerson(person){
console.log(person.firstName ++ person.lastName);
}
函数列表(){
var contactsLength = contacts.length;
for(var i = 0; i< contactsLength; i ++){
printPerson(contacts [i] );
}
};
/ *创建搜索功能
然后称它为Jones* /
函数搜索(lastName){
var contac tsLength = contacts.length;
for(var i = 0;我< contactsLength; i ++){
if(contacts = person.Lastname){
printPerson(contacts [i]);
}
}
};
搜索(jones);
#---------- -----------------
我在想我的代码是搜索功能。对于搜索功能,我想通过拨打他的姓氏来联系我的联系人,并仅从我的联系人那里接收该人。为什么这段代码有问题?如果可以,请帮助调试此代码。谢谢你的时间!
[重新发布 [] - SA]
I'm working on a simple Address book. Here is my code:
var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "[email protected]"
};
var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "[email protected]"
};
var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
};
/*Create a search function
then call it passing "Jones"*/
function search(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength;i++) {
if(contacts = person.Lastname) {
printPerson(contacts[i]);
}
}
};
search("jones");
#---------------------------
I'm thinking the problem with my code is the search function. For the search function, I want to get a person in my contacts by calling his last name, and receiving only that person from my contacts. Why is there a problem with this code? If you could, please help be debug this code. Thank you for your time!
[Re-posted WHy does this happen? Js[^] — SA]
推荐答案
这篇关于JavaScript:帮助调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!