简体   繁体   中英

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.

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.

If prop does not correspond to any valid properties of contact found to match name then return the string No such property.

why it is not working if I use && instead of nested if statement

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

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' . 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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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