If和Return语句的问题

时间:2018-04-29 23:16:49

标签: javascript

我的错误在哪里?我知道它与我的if语句与循环相关的位置有关,但我不能指责它。目标是使函数检查以查看对象是否具有某个属性,然后返回该属性。

//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 < contacts.length; i++) {

    if (contacts[i].firstName === firstName) {
     if (contacts[i].hasOwnProperty(prop)) {
       return contacts[i][prop];
     }   
   } 

   else if (firstName !== contacts[i].firstName) {
     return "No such contact";
   }

   return "No such property";
 } 




// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");

4 个答案:

答案 0 :(得分:2)

正如我在评论中所说,您需要移动失败条件,以便for循环能够在返回之前检查必要的索引:

&#13;
&#13;
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) {
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      }

      return "No such property";
    }
  }

  return "No such contact";
}

console.log(lookUpProfile("Kristian", "lastName"));
&#13;
&#13;
&#13;

或者,您可以使用Array#find()

使用功能稍强的编程方法

&#13;
&#13;
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) {
  const contact = contacts.find(
    contact => contact.firstName === firstName
  );
  
  if (contact === undefined) {
    return "No such contact";
  }
  
  if (contact.hasOwnProperty(prop)) {
    return contact[prop];
  }
  
  return "No such property";
}

console.log(lookUpProfile("Kristian", "lastName"));
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您的陈述return "No such property";应该在for循环之外。在for循环内部,该语句在第一次迭代后终止循环。

此外,您不需要else if (...,因为您是从for循环外返回的。

//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) {

 for (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === firstName) {
   if (contacts[i].hasOwnProperty(prop)) {
     return contacts[i][prop];
   }   
  } 
 } 

 return "No such property";
}

// Change these values to test your function
console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Sherlock", "lastName"))

OR:如果您对Array#filter感到满意:

//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) {
  var res = contacts.filter(p => p.firstName === firstName);
  if (res.length > 0 && res[0].hasOwnProperty(prop)) {
   return res[0][prop];
  } 

 return "No such property";
}

// Change these values to test your function
console.log(lookUpProfile("Kristian", "lastName"))
console.log(lookUpProfile("Sherlock", "lastName"))

答案 2 :(得分:0)

我在Mamun之前已经写完了大部分内容,但我想更多地谈谈这个答案以及你在做什么

对于我的解决方案,我只是将数组过滤到只有具有正确名字的东西。如果您确定您的元素将具有不同的名字,那么这是一个很好的解决方案,因为您应该只期望过滤后的数组的长度为0或1.现在,缺点是Array.filter()运行整个阵列的整个长度,即使在我们找到了我们的人之后仍在继续。对于短数组,如示例,这不是问题。对于包含10,000个元素的数组,我们的人员是第一个搜索到的性能问题。

另一个问题是:如果您有多个具有相同名字的人,会发生什么?那么你的过滤数组将大于1,但我们只会使用第一个结果 - 可能是个问题。

此外,如果您计划经常查找信息,您可能需要考虑使用所谓的“哈希映射” - 这些都是具有复杂声音名称的相当简单的事情。

是什么让一个条目与另一个条目相同?在大多数情况下,我们有来自数据库的数据的各种ID,但在这种情况下,我们没有。然后我们必须问自己是什么让“Kristian Vos”与“Krtistian Paule”不同(假设两者都在相同的数据中)。使用我们所知道的有关对象的重要信息(在本例中为first + last name),我们创建了一个唯一的密钥来将数据存储在对象中。这样,只需使用我们的映射键访问Object,我们就可以更快地查找内容。

我的观点是:小心考虑你将如何处理你的解决方案,并了解小数据集通常很便宜,不断横向,但想象由于某种原因你的数组包含10,000或100,000个条目 - - 你是以最合理的方式处理数据吗?

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){
  let profile = contacts.filter(person=> person.firstName == firstName);
  //Get only the person who has the same first name
  if(!profile.length) return false;
  //If the length is 0, then !0 will be truthy and the function will immediately return false
  profile = profile[0];
  //this is pure laziness, you could just alter the next line to read profile[0][prop] but I'll be honest i did this for visual appeal
  return profile[prop] || false;
  // this notation is fucky -- if the property is set on the profile, then it will return that value.
  // if it is not set, it'll return undefined, which is a falsey value and will therefore move over to the other side of the || and return that value, which is false.
}

console.log(lookUpProfile('Kristian', 'lastName'));

答案 3 :(得分:0)

您可以应用过滤方法并检查假名值

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) {
  let filtered=contacts.filter(c=>c.firstName==firstName);
  return filtered.map(f=>f[prop] || 'not found');
}
console.log(lookUpProfile("Kristian", "lastName"));

如果您只想返回一个条目,可以使用find

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) {
  let filtered=contacts.find(c=>c.firstName==firstName);
  return filtered[prop] || 'not found';
}
console.log(lookUpProfile("Kristian", "lastName"));