I’ve cleaned up your code a bit. What do you see if you try this?
function lookUpProfile(name, prop) {
// Only change code below this line
var valueToReturn;
for (var i = 0; i < contacts.length; i++) {
var currentFirstName = contacts[i].firstName;
console.log("--------------------");
console.log("i is:", i);
console.log("current first name is:", currentFirstName);
// Checking name
if (currentFirstName != name) {
valueToReturn = "No such contact";
}
// Checking property
if (currentFirstName == name && contacts[i].hasOwnProperty(prop)) {
valueToReturn = contacts[i][prop];
} else if (contacts[i].hasOwnProperty(prop) == false) {
valueToReturn = 'No such property';
}
console.log("return value is now:", valueToReturn);
}
return valueToReturn;
// Only change code above this line
}
lookUpProfile("Akira", "likes");
Sorry I don’t mean remove I mean it only displays the index of Akira and if I change the operator to == it doesn’t display the Akira index whose value is true
You still change the value of x (what I renamed valueToReturn) on every single loop iteration. So it must match the last update you made, which only considers the very last contact. This isn’t what you want.
Ok so I ran your code. The way you made a little template using the console log is very cool btw, I’ll be labeling my logs like that from now on.
The strange output still persists in the //checking name block though.
I ended up changing the format of my solution overall to fix the problem.
I’m still not sure why the iteration was being affected by the comparison operator but I decided to move on for now.
The iteration is not “being affected by the comparison operator”. As it was written above, you kept changing the valueToReturn (x), so the data in that variable matched the last change.
Your logic should express that you need to stop and return if you find a matching name and return something different if you make it through the entire loop without finding a matching name.