Basic JavaScript - Profile Lookup

Tell us what’s happening:

It fails tests 4 and 5 and I’m not sure if I’m missing something or if I’m just getting screwed over because this isn’t the intended way to solve this

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

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15

Challenge Information:

Basic JavaScript - Profile Lookup

Try running the failing case. That will give you a big hint

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

lookUpProfile("Bob", "number");

I really would avoid a while loop unless you really need it, but any valid code will pass.

1 Like

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