Help with Basic JavaScript: Profile Lookup

Help with Basic JavaScript: Profile Lookup
0.0 0

#1

I don’t understand what I’m doing wrong here. I am passing the “No such contact” and “No such property” requirements but am failing to return the values of the properties. It should return the values in the “return contacts[x][prop];” line but it isn’t for some reason. Can somebody point out what I’m missing here?

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

#2

It will help to identify the problem when your code is indented properly.

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

Your solution only makes it through one iteration of the for loop before you return a value. Remember, once a return statement is executed, the function exits immediately.

Hint: Think carefully about when/where you should return ‘No such contact’.


#3

Oh man, I see where I was wrong now. It was returning “No such contact” before it had the chance to loop again. So all I had to do was put the return “No such contact” outside of the for statement. Thank you!