简体   繁体   English

Javascript 配置文件查找挑战

[英]The Javascript Profile Lookup challenge

I've been stuck here for a while, and I checked on other solutions which matched mine perfectly.我被困在这里有一段时间了,我检查了其他与我的完美匹配的解决方案。 But I can't seem to find the error in my code as it keeps saying..但我似乎无法在我的代码中找到错误,因为它一直在说......

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

This is the challenge...这是挑战...

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"],
  },
];

My code...我的代码...


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");

The test parameters测试参数

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

Please I'd be so glad if anyone can help open my eyes.如果有人能帮我睁开眼睛,我会很高兴。

You were prematurely ending your function if you didn't find the contact in the first iteration.如果您在第一次迭代中没有找到联系人,那么您就提前结束了您的 function。 You were close to getting it to work:你已经接近让它工作了:

 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") );

I'd recommend that you use Array.prototype.find() , optional chaining (?.) and the nullish coalescing operator (??) .我建议您使用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