Profile Lookup not sure what is wrong

Profile Lookup not sure what is wrong
0.0 0

#1

Tell us what’s happening:
My code works when the arguments are not found, but not when they are. Any ideas?

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
    for (var i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
            var propVal = contacts[i].prop;
           return propVal;
    } else if (contacts[i].firstName !== name) {
        return "No such contact";
    } else if (contacts[i].prop !== prop) {
        return "No such property";
    }
    }
// Only change code above this line
}

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

Your browser information:

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

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


#2

Your for loop only makes one iteration before returning some value. As soon as the return statement is executed, the function immediately exits.

Hint: Think about when/where you should return “No such contact”.


#3

Oh, thanks. So the “No such…” should come first?


#4

Work through a sample test and test your algorithm to see if that makes sense.

Answer this question: At what point will you know for sure that the name is not in the contacts array?


#5

I am not sure, but it seems like the for loop would make it so that it would run through all four entries right away and know then whether the name is in the array, I mean, if I start with this:

        if  (contacts[i].firstName !== name) {
        return "No such contact";  
    } else if (contacts[i].prop !== prop) {
        return "No such property";

#6

Maybe there is something about a for loop I do not understand. I thought it keeps iterating, unless an if statement is true and then a value is returned. no?


#7

I think you are not understanding what happens with the return statement. When a return statement executes, the function is exited and does not come back to finish iterating a loop of any kind. For example, the following code will not display the numbers 1 through 100. Instead, it will only display the value 1 and then stop, because of the return statement.

for (var i = 1; i <= 100, i++) {
  console.log(i);
  return "this will stop the for loop and the function will exit";
}

#8

Well, actually, I did understand that a return ended the loop. But if the if statement is false, does it keep iterating?


#9

What I mean is if (contacts[i].firstName !== name) is false, does it not move to the next else if statement?


#10

Yes, if the first if statement condition evaluates to false, then the next if statement condition will be evaluated.


#11

OK, so if I arrange it as follows, why is it not executing the third return statement when the firstName and prop are present in the array?:

function lookUpProfile(name, prop){
// Only change code below this line
    for (var i = 0; i < contacts.length; i++) {
        if  (contacts[i].firstName !== name) {
        return "No such contact";  
    } else if (contacts[i].prop !== prop) {
        return "No such property";
    } else if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) {
        var propVal = contacts[i].prop;
           return propVal;
    }
    }
// Only change code above this line
}

#12

write 'No such property" first
works for me


#13

Let’s walk through your code with the test case of lookUpProfile(“Kristian”, “lastName”).

The for loop starts with i = 0. The first if statement condition evaluates to true because contacts[0].firstName is “Akira” and name is “Kristian”. Since your if statement condition asks "Is “Akira” NOT equal to “Kristian”, the answer is Yes so the condition evaluates to true and you return “No such contact”.


#14

Thanks, Randell. Now I understand.