Profile Lookup(not behaving as expected)

Tell us what’s happening:

I am trying to solve the challenge Basic JavaScript: Profile Lookup but everytime i run it I get the output ‘’. The loop runs OK and even goes inside the first if condition :

if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop))

and checks for the condition and even return the value ** “[“Intriguing Cases”,“Violin”]” ** in console.log. But somehow it doesn’t passes the test with the same condition.

Any Idea How could that happen?

Your code so far


//Setup
var 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
console.log(contacts.length)

var i = 0
var returnValue = "";

while ( i < 4 ) {
  console.log("i inside for loop " + i)
  // console.log("Name before if: " + name)
  // console.log("Property before if: " + prop)
  // console.log("Value of the Property before if loop: " + JSON.stringify(contacts[3].lastName) )

  console.log("name before if loop " + name)
  console.log("contacts[i] before if loop" + JSON.stringify(contacts[i]) )
  console.log("contacts[i].firstname before if loop " + JSON.stringify(contacts[i].firstName))
  console.log("contacts[i].hasOwnProperty(prop) before if loop " + contacts[i].hasOwnProperty(prop))
  console.log(" 'i' before if loop " + i)

  if (name === contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {

    console.log("name inside if loop " + name)
    console.log("contacts[i] inside if loop" + JSON.stringify(contacts[i]) )
    console.log("contacts[i].firstname inside if loop " + JSON.stringify(contacts[i].firstName))
    console.log("contacts[i].hasOwnProperty(prop) inside if loop " + contacts[i].hasOwnProperty(prop))
    console.log(" 'i' inside if loop " + i)
    // return contacts[i].prop
    // console.log("i inside if loop" + i)
    // console.log(" name inside if loop " + JSON.stringify( contacts[i].firstName )) 
    console.log("contacts[i] inside if loop " + JSON.stringify(contacts[i]))
    console.log("prop inside if loop " + prop)
    
    var jsonValue = (contacts[i][prop]);
    returnValue = JSON.stringify(jsonValue)
    console.log("returnValue " + returnValue)
  }
  else if ( name != contacts[i].firstName ) {
    returnValue =  "No Such contact"
  }
  else if (prop != contacts[i].hasOwnProperty(prop)) {
    returnValue =  "No such property"
  }
  i++;
  console.log("i at the end of for loop " + i)

}
return returnValue
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Sherlock", "likes");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

try adding a console.log(returnValue) just before the return statement
because you can print to the console anything, but what matters is the returned value

an other thing you should consider is that doing this will make things like arrays and objects into strings, but if the property value is that of array then you should return the array