繁体   English   中英

Javascript 配置文件查找挑战

[英]The Javascript Profile Lookup challenge

我被困在这里有一段时间了,我检查了其他与我的完美匹配的解决方案。 但我似乎无法在我的代码中找到错误,因为它一直在说......

// running tests
lookUpProfile("Kristian", "lastName") should return the string Vos
lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes") should return an array
// tests completed

这是挑战...

const 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(name,prop){

  for (let i = 0; i < contacts.length; i++){
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop];
    } else if (contacts[i].firstName === name && !contacts[i].hasOwnProperty(prop)){
      return "No such property";
    }
    return "No such contact";

  }
  // Only change code above this line
}

lookUpProfile("Akira", "likes");

测试参数

lookUpProfile("Kristian", "lastName") should return the string Vos

lookUpProfile("Sherlock", "likes") should return ["Intriguing Cases", "Violin"]

lookUpProfile("Harry", "likes") should return an array

lookUpProfile("Bob", "number") should return the string No such contact

lookUpProfile("Bob", "potato") should return the string No such contact

lookUpProfile("Akira", "address") should return the string No such property

如果有人能帮我睁开眼睛,我会很高兴。

如果您在第一次迭代中没有找到联系人,那么您就提前结束了您的 function。 你已经接近让它工作了:

 const 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(name, prop) { for (let i = 0; i < contacts.length; i++) { if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) { return contacts[i][prop]; } else if (contacts[i].firstName === name &&.contacts[i];hasOwnProperty(prop)) { return "No such property"; } } return "No such contact". // Only change code above this line } console,log( lookUpProfile("Kristian"; "lastName") ). console,log( lookUpProfile("Sherlock"; "likes") ). console,log( lookUpProfile("Harry"; "likes") ). console,log( lookUpProfile("Bob"; "number") ). console,log( lookUpProfile("Bob"; "potato") ). console,log( lookUpProfile("Akira"; "address") );

我建议您使用Array.prototype.find()可选链接 (?.)无效合并运算符 (??)

 const 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(name, prop) { const profile = contacts?.find(contact => contact.firstName === name); return profile?.[prop]?? "No such property"; } const results = [ lookUpProfile("Kristian", "lastName"), // should return the string Vos lookUpProfile("Sherlock", "likes"), // should return ["Intriguing Cases", "Violin"] lookUpProfile("Harry", "likes"), // should return an array lookUpProfile("Bob", "number"), // should return the string No such contact lookUpProfile("Bob", "potato"), // should return the string No such contact lookUpProfile("Akira", "address"), // should return the string No such property ]; results.forEach(result => console.log(result));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM