Basic JavaScript - Profile Lookup

Tell us what’s happening:
Describe your issue in detail here.
I’m not sure what i did wrong here;
I got the output
// 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("Akira", "address")

should return the string

No such property

// tests completed

  **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 (var i = 0; i < contacts.length; i++) {
if (contacts[i].firstName === name) {
  if(prop in contacts[i]) {
    return contacts[i][prop] || "No such property";
    }
// Only change code above this line
  }
  return "No such contact";
}
}
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/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Profile Lookup

Link to the challenge:

This condition is not needed you only need the condition of matching the name, if the contacts.firstName matches with name it will return all the props

I removed that but I’m still getting:

lookUpProfile("Kristian", "lastName")

should return the string

Vos
lookUpProfile("Sherlock", "likes")

should return

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

should return an array // tests completed

that’s a correct return statement!


let’s take a look at what this code returns adding a console.log()

const output = lookUpProfile("Kristian", "lastName");
console.log({ output })

the output is No such contact, it shouldn’t be

let’s investigate some more, with more console.log

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

    console.log({
      i,
      'contacts[i].firstName': contacts[i].firstName,
      'contacts[i][prop]': contacts[i][prop]
    })
    
    if (contacts[i].firstName === name) {
      if (prop in contacts[i]) {
        return contacts[i][prop] || "No such property";
      }
      // Only change code above this line
    }
    return "No such contact";
  }
}

in the console appear this

{ name: 'Kristian', prop: 'lastName' }
{ i: 0,
  'contacts[i].firstName': 'Akira',
  'contacts[i][prop]': 'Laine' }
{ output: 'No such contact' }

It seems like i is only ever 0, it never appears to be higher numbers - can you figure out what stops your loop?


A couple of extra things:

using short circuiting can be useful, but note that if contacts[i][prop] has a value as 0 or null, you don’t get the value but No such property, it’s not an issue in this case as all property values in the object are truthy, but keep it in mind.

Also if you are interested

this is called object property shorthand

(post deleted by author)

that doesn’t make any difference with OP’s issue

but please don’t try to give answers to copy and paste

if(prop in contacts[i]) {
    return contacts[i][prop] || "No such property";
}

Looking at this part of the code the condition checks if contact[i] has a particular property, the only time you want that block to run is if the object has the property; whether the property is false, falsey, true or truthy we want the property either way. Therefore you will need another case or condition for when the above is not matched.

Your indentation is incorrect, after removing

if(prop in contacts[i]) {

you have to fix the indentation, make sure it’s indented accordingly

for () {
 if () {
  return
 }
}

as much as indentation makes things more readable, it is not necessary for code correctness