Basic JavaScript - Profile Lookup

Tell us what’s happening:

Why is my code not fulfilling the “no such contact” tests? It fulfills the others. I ran it through ChatGPT to see if the output would give “no such contact” for (“Bob”, “number”) and it did, but it doesn’t on the test. It’s my 4th day coding be gentle :slight_smile:

EDIT: I removed the “=” from “i <= contacts.length” and it worked, but I don’t fully understand why. If someone could explain that’d be awesome. Thanks!

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 i = 0; i <= contacts.length; i++){
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop)){
      return contacts[i][prop]
    }else if (!contacts[i].hasOwnProperty(prop)) {
      return "No such property"   
} 
}
}
return "No such contact"

  // Only change code above this line
}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Profile Lookup

It helps if you use more conventional formatting

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

question - why did you pick this for loop bound for (let i = 0; i <= contacts.length; i++) {

Note - you should be seeing this error message

TypeError: contacts[i] is undefined

This is a big hint!

Hey, thanks for the reply!

It’s only my fourth day of programming so I’m still working on the formatting bit, but thanks for the advice!

I removed the “=” from “i <= contacts.length” and it worked, but I don’t fully understand why.

As for your question, I just finished the loops section so I figured this test would require me to use a loop. I’m not sure what exactly your question is asking, but my answer is it was an education guess?

I agree you should use a for loop. My question is why you picked <= contacts.length

I thought that it would work like an array. So if I had arr.length, it would iterate through until it reached the end of the array which is “.length”. Sorry if my explanation is poor, but I think that answers your question.

Edit:: So in this case, contacts is the array.

Right, but what’s the first index of an array? (its where you started i)

The first index is 0.

So, if you start at 0 and go up to 5 (lets say our contacts.length === 5), then you have
0
1
2
3
4
5

how many numbers is that?

6, which would be more than the length of the array. I see what you mean. That’s a silly mistake. Thank you for the help!

1 Like

Its a easy one to overlook - “Off by 1” is one of the most common mistakes with loops!

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