This code is not executing even if the conditions are correct

Tell us what’s happening:

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

var  properties;
for (var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === name ){
    properties = contacts[i][prop];
    console.log(name,properties);  // the problem is here
}else{
    return("No such contact");
}
}
return("No such property")

}




// Only change code above this line
var a = lookUpProfile("Harry", "lastName");
console.log(a);


Here even if name and prop is true, that line is not executing.
until i remove the else statement like this:

function lookUpProfile(name, prop){
// Only change code below this line

var  properties;
for (var i = 0; i < contacts.length; i++){
    if(contacts[i].firstName === name ){
        properties = contacts[i][prop];
        console.log(name,properties);  
    }
}

}




// Only change code below this line
lookUpProfile("Harry", "lastName"); //Harry potter

Challenge: Profile Lookup

Link to the challenge:

Remember that as soon as a return statement is hit, function execution ends. Your code only checks the first vale of contacts because it returns a property if the name matches firstName of contacts[0]. If the name doesn’t match for that first item, then your code returns “No such contact”.

2 Likes

What i’m created is :slight_smile:

After loop for (var i = 0; i < contacts.length; i++) the code search for contacts[i].firstName === name

so loop is not executing after it found the contact is true. it even check for name == true.

And the above code works correctly after removal of else statement.

Exactly, which means that the else statement is doing something other than what you expect. In this case, the else statement is forcing your function to return the very first time you find a contact that has a firstName that is different than name.

1 Like

Yes it execute first name correctly.
but it also execute else statement with that.

lookUpProfile("Akira", "lastName");

//Akira Laine
//No such contact

so it is executing both if and else conditions.

Now how to solve it ?

The code

if (contacts[i].firstName === name ){
    properties = contacts[i][prop];
    console.log(name,properties);  // the problem is here
} else {
    return("No such contact");
}

says, in words, if contacts[i].firstName is name, then log name and properties to the console. Otherwise immediately stop executing this function and return the string No such contact".

This means that in this code

for (var i = 0; i < contacts.length; i++) {
  if(contacts[i].firstName === name ){
      properties = contacts[i][prop];
      console.log(name,properties);  // the problem is here
  } else {
      return("No such contact");
  }
}

The very first time you encounter a contacts[i].firstName that is not name, you immediately halt the evaluation of the function and return "No such contact".

I think the problem you may be trying to pin down is that there is no return statement in your if block (which I didn’t notice at first). If the name matches it will print and go on to the second element in contacts. That second name wont match so it will return “No such contact”.

But the “i” is inside the loop of “for”.
and there are 4 objects in the array of contacts
so “i” became 0,1,2,3.
and if contacts[0] has a firstName ===name or,
contacts [1] has a firstName ===name or,
contacts [2] has a firstName ===name or,
contact s[3] has a firstName ===name

it executes that name. because it is inside that for loop .

so here in contacts[i], i != 0.
it should changes from i=0 to i = 3.

is that how it works?

But the “i” is inside the loop of “for”.
and there are 4 objects in the array of contacts
so “i” became 0,1,2,3.
and if contacts[0] has a firstName ===name or,
contacts [1] has a firstName ===name or,
contacts [2] has a firstName ===name or,
contact s[3] has a firstName ===name

it executes that name. because it is inside that for loop .

so here in contacts[i], i != 0.
it should changes from i=0 to i = 3.

is that how it works?
[/quote]

You’ve made a snarl of logic here.

The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.
If both are true, then return the “value” of that property.
If name does not correspond to any contacts then return "No such contact" .
If prop does not correspond to any valid properties of a contact found to match name then return "No such property" .

Your conditionals don’t match this. You have

If name is the first contact’s firstName, then log the name and properties to the console.
Otherwise, return the string "No such contact".

As soon as (contacts[i].firstName === name) is false, your function returns and stops executing.

Is there any features needed like, if there are 2 properties needed to be true so there should be 2 if statement should be true?

Maybe this will help make it clearer.

An if-else can always be rewritten.

if (condition) {
  // Do stuff
} else {
  // Do other stuff
}

is the same as

if (condition) {
  // Do stuff
}
if (!condition) {
  // Do other stuff
}

In your case, your statement is

if (contacts[i].firstName === name) {
  properties = contacts[i][prop];
  console.log(name,properties);
}
if (contacts[i].firstName !== name) {
  return("No such contact");
}

I don’t think this is what you mean to be doing.

there is a bug in the line var h = console.log(name,properties);
Even the below code executes correctly in google console, it shows a bug.
And i don’t know the debugging. I haven’t reached there.

function lookUpProfile(name, prop){
// Only change code below this line

var  properties;
for (var i = 0; i < contacts.length; i++){
if(contacts[i].firstName === name ){
    properties = contacts[i][prop];
    var h = console.log(name,properties);  // the problem is here
    return h; 
}
}
}



// Only change code above this line
lookUpProfile("Harry", "lastName");

Why do think there is a bug in that line? Console logging does not have a return value so you aren’t setting h to anything.

As Jeremy said, console.log doesn’t return a value so h is going to be undefined.

Like I said,

Here is what I recommend you do.

Step away from the keyboard. Seriously. Don’t touch it! Touching the keyboard too quickly is a great way to write bugs. Grab a piece of paper.

Now, look at the challenge. (ok, you might have needed to touch the keyboard to see the challenge, but stop touching it right now!)

What are the three expected exit states for this code? Write them down on a piece of paper. There are only 3 different things that this code is supposed to return.

Now that you know the three exit states, what are the requirements for each one. No! Don’t go type them out. Put them on the piece of paper.

From here, you can short thinking about if statements. Make a flow chart. If you find a matching first name, what are the two possible outcomes? How do they differ? If you don’t find a matching first name, then what is the expected outcome?

Now you can touch the keyboard, but only to write comments. No code yet! Write comments describing what code you need.

OK, now try to write code.

I’m serious. You seem to be pushing keys without a clear target in mind and you are making silly mistakes as a result. We’ve all done it. This is how you fix it.

1 Like

Please have a look at thee screenshot:

this code is executing correctly.
But it also execute both else and if statement at the same time!

No it didn’t.

The first item in contacts has the firstName “Akira”.
“Harry” is not equal to “Akira”.
So the else executes and “No such contact” is printed.
Then the loop moves to the second item in contacts.
The second item in contacts has the firstName “Harry”.
“Harry” is equal to “Harry”.
So the if block executes and “Harry [‘Hogwarts’, ‘Magic’, ‘Hagrid’]” is printed.
Then the return is hit.
The function is done.

It sounds like you haven’t gotten comfortable with loops yet. You might want to go back and review earlier lessons where for loops were taught.

I don’t think you listened to my advice. You are pushing around code without a plan and it’s making a mess for you. I recommend going back to the drawing board, as I described above.