Comparison Operator Removes True Value in For Loop Iteration

I think I see what you are trying to do.

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

The console.log() statement does not “remove” anything. It has no ability to change the array in any way.

I’ll have to test it when I get back to my laptop unfortunately that won’t be for a couple hours

1 Like

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.

1 Like

Ok I’ll take a look at it when I get home…

Thanks

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.

Thanks again

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.

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