简体   繁体   English

使用 for 循环和 if 条件查找配置文件

[英]Profile Look Up using for loop and if condition

A lookUpProfile function that takes a name and property (prop) as arguments has been pre-written for me.已经为我预先编写了一个lookUpProfile function,它采用名称和属性(prop)作为arguments。

If both are true, then return the "value" of that property.如果两者都为真,则返回该属性的“值”。

If the name does not correspond to any contacts then return the string No such contact.如果姓名不对应任何联系人,则返回字符串 No such contact。

If prop does not correspond to any valid properties of contact found to match name then return the string No such property.如果 prop 不对应于找到匹配名称的联系人的任何有效属性,则返回字符串 No such property。

why it is not working if I use && instead of nested if statement如果我使用 && 而不是嵌套的 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";
}
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
}

This function will always return "No such contact" if contacts[0] is not the value you want to get.如果 contacts[0] 不是您想要获取的值,则此 function 将始终返回“No such contact”。

Below code will work.下面的代码将起作用。

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
}

The problem is these lines问题是这些行

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

you are basically saying if the given conditions don't match then return the function with 'No such contact' .您基本上是在说如果给定条件不匹配,则返回 function 并带有'No such contact' This means the loop stops iterating after the first iteration.这意味着循环在第一次迭代后停止迭代。 Now since the function is returned the loop ends meaning you are only ever checking the first index ie [0] .现在由于返回 function 循环结束,这意味着您只检查第一个索引,即[0]

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

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