【问题标题】:Profile Look Up using for loop and if condition使用 for 循环和 if 条件查找配置文件
【发布时间】:2022-01-13 05:29:50
【问题描述】:

已经为我预先编写了一个将名称和属性 (prop) 作为参数的 lookUpProfile 函数。

如果两者都为真,则返回该属性的“值”。

如果姓名不对应任何联系人,则返回字符串 No such contact。

如果 prop 不对应于找到匹配名称的联系人的任何有效属性,则返回字符串 No such property。

如果我使用 && 而不是嵌套的 if 语句,为什么它不起作用

// Setup
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) {
  // Only change code below this line
  for (let i = 0; i <= contacts.length; i++) {
    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else return "No such contact";
  }
  return "No such contact";
  // Only change code above this line
}

lookUpProfile("Akira", "likes");



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

【问题讨论】:

    标签: javascript arrays loops for-loop if-statement


    【解决方案1】:
    function lookUpProfile(name, prop) {
      // Only change code below this line
      for (let i = 0; i <= contacts.length; i++) {
        if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
          return contacts[i][prop];
        } else return "No such contact";
      }
      return "No such contact";
      // Only change code above this line
    }
    

    如果contacts[0]不是你想要得到的值,这个函数总是会返回“No such contact”。

    下面的代码可以工作。

    function lookUpProfile(name, prop) {
      // Only change code below this line
      for (let i = 0; i <= contacts.length; i++) {
        if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
          return contacts[i][prop];
        } 
      }
      return "No such contact";
      // Only change code above this line
    }
    

    【讨论】:

    • 它解决了一个问题,但第二个问题仍然很复杂。哪个是没有这样的财产
    • @OwaliUllahShawon 在第二个函数中删除以下句子:else { return "No such property"; }
    【解决方案2】:

    问题出在这几行

    if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
          return contacts[i][prop];
        } else return "No such contact";
    

    您基本上是说如果给定条件不匹配,则返回带有'No such contact' 的函数。这意味着循环在第一次迭代后停止迭代。现在,由于返回函数,循环结束意味着您只检查第一个索引,即[0]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-29
      • 2012-08-18
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 2015-08-24
      • 2021-06-07
      相关资源
      最近更新 更多