Basic JavaScript - Profile Lookup

Tell us what’s happening:
I copied your code from the sample solution, but it doesn’t pass the test in your simulator :neutral_face: :neutral_face:

Your code so far

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";
}

I also tried my solution, it also fails with the same errors

  function lookUpProfile(name, prop) {
    // Only change code below this line
    
for (i = 0; i<contacts.length; i++) {

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

    // Only change code above this line
  }

// running tests

lookUpProfile("Kristian", "lastName") 

should return the string

Vos
lookUpProfile("Sherlock", "likes")

should return

["Intriguing Cases", "Violin"]
lookUpProfile("Harry", "likes")

should return an array

lookUpProfile("Bob", "number")

should return the string

No such contact
lookUpProfile("Bob", "potato")

should return the string

No such contact
lookUpProfile("Akira", "address")

should return the string

No such property

// tests completed

Your browser information:

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

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

Let’s apply some standard formatting

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

This looks close to me, but why are you using String()? The instructions don’t ask you to force the value of the property to become a string before you return it?

Gotta be careful with contacts[i][prop] though - that checks if the property is truthy, not if the property exists. You should use the new syntax shown in the Challenge.

Also, where did the contacts variable go?

1 Like

Thanks, I understand why your solution didn’t work. All that’s left is to figure it out. Sorry for such a stupid post

What’s “your solution”? I didn’t write a solution here?

I mean the solution taken from here

it didn’t work because I accidentally erased contacts when copying and pasting

Ah. Yeah. Its not really recommend that you copy-paste those to try to pass.

I didn’t even think about doing that. I just wanted to check if the example code would work after mine didn’t work.

Thank you, I realized what my mistake was. It was necessary to use contacts[i][prop] to check the value. To check you need to use contacts[i].hasOwnProperty(prop). now my solution passes :slightly_smiling_face:

1 Like

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