Basic JavaScript - Profile Lookup

###Can i solve this question without using the for loop like in the Record Collection video by only using if…else…if statements and if then how?

I wan’t to know how to not use loop variables in the code below by making the code run through if…else…if statements. Can you help me by giving an answer as such?

Your code so far

// 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 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";
  // Only change code above this line
}

console.log(lookUpProfile("Akira", "address"));
console.log(lookUpProfile("Kristian", "lastName"));
console.log(lookUpProfile("Sherlock", "likes"));
console.log(lookUpProfile("Bob", "number"));
console.log(lookUpProfile("Bob", "potato"));

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Profile Lookup

You appear to have created this post without editing the template. Please edit your post to Tell us what’s happening in your own words.

  • you are dealing with an “array” of objects here, right?
  • given that, how else would you consider traversing through each object?

happy coding :slight_smile:

but switch statement could have been used right???

you could use many things, it doesn’t mean that they are the best instrument for the situation. How would you use the switch here?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.