所以我通过freecodecamp解决了那里的问题,与编程保持一致,我偶然发现了一个障碍,我并不完全确定有什么不对。
所以我有一个名为contacts的对象数组,我需要创建一个名为lookUp(firstName, prop)
的函数。作业的文本是这样的:
该函数应检查
firstName
是否为实际联系人firstName
,并且给定属性(prop
)是该联系人的属性。如果两者都为真,则返回"值"该财产。
如果
firstName
与任何联系人不对应,则返回"没有此类联系人"如果
prop
与任何有效的属性不对应,则返回"没有此类属性"
代码:
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intruiging Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
},
];
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( contacts[i].firstName == firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i].prop;
} else {
return "No such property";
}
} else {
return "No such contact";
}
}
}
// Change these values to test your function
lookUp("Kristian", "lastName");
所以我循环遍历数组,for
循环检查每个对象。在第一个if
中,我检查该对象的firstName
属性是否等于函数参数firstName,如果它是真的,我检查该对象是否具有属性prop
,我应该能够归还它。但似乎
return contacts[i].prop;
不起作用,我有点迷失为什么。我确定它是微不足道的,但我不明白为什么。当我去控制台,并测试
contacts[0].likes
我离开了数组["Pizza", "Coding", "Brownie Points"]
,但是如果我不能正常工作的话。我在这里做错了什么?
修改
好的,我试过
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( contacts[i].firstName == firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
} else {
return "No such contact";
}
}
}
但我仍然得到同样的错误:\
答案 0 :(得分:9)
好的我傻了,我太早退出了我的循环:
function lookUp( firstName, prop ){
for( var i = 0; i < contacts.length; i++ ){
if( firstName == contacts[i].firstName ) {
if( contacts[i].hasOwnProperty( prop ) ) {
return contacts[i][prop];
} else {
return "No such property";
}
}
}
return "No such contact";
}
这很有效。
答案 1 :(得分:4)
我认为你对返回的做法感到困惑。您的代码只会执行一次迭代return "No such contact"
。返回立即停止执行的功能。这是我用console.log
https://jsfiddle.net/oegw3a4y/
在您的情况下,第一次迭代在第一个if
语句中的计算结果为false,并立即转到else
。
答案 2 :(得分:3)
function lookUp(firstName, prop){
var ans="";
for(var i=0;i<contacts.length;i++){
//if firstname matches,fetch the property
if(contacts[i].firstName==firstName)
{
//in the item matched with first name check if the property is there or not.
if(contacts[i].hasOwnProperty(prop))
{
ans=contacts[i][prop];
}
else{
ans="No such property";
}
}
}
// if no contact found after searching entire collection.then ans will be empty
if(ans==="") ans="No such contact";
return ans;
}
答案 3 :(得分:0)
返回调用需要是堆栈中的最后一件事。上面的代码不起作用,因为在循环迭代到下一个增量之前函数返回“No such contact”!以下作品:
//Setup
var contacts = [
{
"firstName": "Akira",
"lastName": "Laine",
"number": "0543236543",
"likes": ["Pizza", "Coding", "Brownie Points"]
},
{
"firstName": "Harry",
"lastName": "Potter",
"number": "0994372684",
"likes": ["Hogwarts", "Magic", "Hagrid"]
},
{
"firstName": "Sherlock",
"lastName": "Holmes",
"number": "0487345643",
"likes": ["Intriguing Cases", "Violin"]
},
{
"firstName": "Kristian",
"lastName": "Vos",
"number": "unknown",
"likes": ["Javascript", "Gaming", "Foxes"]
}
];
function lookUpProfile(firstName, prop){
// Only change code below this line
for ( var i = 0; i <= 3; i++ ) {
if (contacts[i].firstName == firstName) {
if ( contacts[i].hasOwnProperty(prop) === true) {
return contacts[i][prop];
}
return "No such property";
}
}
return "No such contact";
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
答案 4 :(得分:0)
# make gensim dictionary and corpus
dictionary = gensim.corpora.Dictionary(boc_texts)
corpus = [dictionary.doc2bow(boc_text) for boc_text in boc_texts]
tfidf = gensim.models.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
&#13;
答案 5 :(得分:0)
function lookUpProfile(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
if (firstName === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
return contacts[i][prop];
}
}
for (var j = 0; j < contacts.length; j++) {
if (firstName === contacts[j].firstName && contacts[j].hasOwnProperty(prop) === false) {
return 'No such property';
}
}
return 'No such contact';
// Only change code above this line
}
答案 6 :(得分:-1)
以下是我使用迄今为止在FCC中提供的工具所做的工作。
var o = "";
function lookUp(firstName, prop) {
for (i = 0; i <= contacts.length - 1; i++) {
if (firstName === contacts[i].firstName && (contacts[i][prop])) {
return (contacts[i][prop]);
}
}
// if property look up returns undefined type then there is no such property
if ((contacts[0][prop]) === undefined) {
o = "No such property";
} else {
o = "No such contact";
}
return o;
}