0

In the loop I am not sure if I have made a mistake or bad code. When I run the if statement alone, it works. But when I run it along with the "else if " statement. The if statement fails, the else if statement works, even when the first if statement is true.

https://www.freecodecamp.com/challenges/profile-lookup

//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(firstName, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
  if ( contacts[i].firstName === firstName  &&     contacts[i].hasOwnProperty(prop) ) {
    return contacts[i][prop];


}  else if( contacts[i].firstName !== firstName ) {
  return "No such contact";  
    }
  }
// Only change code above this line
}

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

3 Answers 3

1

Move "No such contact" reply outside the loop:

function lookUpProfile(firstName, prop) {
  var match = false;
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName) {
      match = true;
      var valid = contacts[i].hasOwnProperty(prop);
    }
    if (match && valid) {
      return contacts[i][prop];
    }
    if (match && !valid) {
      return "No such property";
    }
  }
  return "No such contact";  
}

Assumption: each person stored in one card.

This code iterates all contacts, and when current contact matches the condition, it will be immediately returned. When the loop ends, it means there are no matching contacts, so return corresponding reply.

I prefer this way of coding without nested if, but you may rewrite inner for code like so:

if (contacts[i].firstName === firstName) {
  if (contacts[i].hasOwnProperty(prop)) {
    return contacts[i][prop];
  } else {
    return "No such property";
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Please explain what you have changed. Giving working code is not good enough
What if I have to add another two else statements with condition?
What conditions you mean?
@max zuber please take a look at the link freecodecamp.com/challenges/profile-lookup
0

An alternate to @Max's answer:

Idea is to have a variable which will be returned. Now in every condition, you just have to set this variable value. This will enable you to put extra logic like getContact, massageContact, if duplicate values exists, merge them or get a specific value etc.

function lookUpProfile(firstName, prop) {
  var match = undefined;
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === firstName && contacts[i].hasOwnProperty(prop)) {
      match = contacts[i][prop];
      break;
    }
  }
  // Do extra stuff here
  return match ? match : "No such contact"
}

An alternate array.find. You can also look into array.filter

Array.find

function lookUpProfile(firstName, prop){
  var o = contacts.find(function(c){ return c.firstName === firstName; });
  if(o && o.hasOwnProperty(prop)) return o[prop];
  else if(o && !o.hasOwnProperty(prop)) return "No such property";
  else return "No such contact";
}

References:

Note: do check their compatibility before using them.

2 Comments

@Downvoter, please comment whats missing in the answer.
LOL! Its ok. forget it.
0

Max Zuber is right, your code reads as this:

With If else statement:
for (var i = 0; i < contacts.length; i++) {

    // if this true, the inside code block will be executed. 
    // You have return statement so, the loop stops and return the data. 
    if ( contacts[i].firstName === firstName  &&       contacts[i].hasOwnProperty(prop) ) {

        return contacts[i][prop];

    // if above fails, it will check this condition. 
    // If this true(which on   your design is always true) the inside code block is executed. 
    // You have return statement so the loop stops and return the message. 
    }  else if( contacts[i].firstName !== firstName ) {
     return "No such contact";  
      }
}

With If statement alone:
for (var i = 0; i < contacts.length; i++) {

    // If this true, stop the loop and return the data. 
    // else, loop continue until no data to be check. 
    if ( contacts[i].firstName === firstName  &&     contacts[i].hasOwnProperty(prop) ) {

      return contacts[i][prop];
      } 
}

// This will only be executed if the above if statement fails. 
return "No such contact";  

Note: When function hit return statement it will stop execution and pass the return statement even if there are data waiting to be check which is what happen to your if else statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.