Profile LookUp Stuck

Profile LookUp Stuck
0.0 0

#1

Hi guys!

I’m stuck on the Profile LookUp Challenge : Here
Could anyone tell me where is my mistake, please? I’ve check different solutions and posts, but I still can’t explain what’s wrong…
Also, is there any way to debug our code? (Like using spies, or at least see what return our function for a given property).

Thank you for your help :slight_smile:

//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) {
    if (contacts[i].hasOwnProperty(prop) == true){
      return contacts[i][prop];
    } else {
      return "No such property";
    }  
  } else {
    return "No such contact";
  }
}
// Only change code above this line
}

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

#2

Hint: Remember, once a return statement is executed, the function is exited and does not return to complete a loop.

To help you see how many iterations your code loops through for each test case, you can use console.log statements to see the values of various variables at different points in your code and then use your browser’s console (Ctrl + Shft + J in Chrome) to see the results. See below for an example of debugging:

function lookUpProfile(name, prop) {
	// Only change code below this line
	console.log('start of function');
	for (var i = 0; i < contacts.length; i++) {
		if (contacts[i]['firstName'] == name) {
			console.log('found ' + name);
			if (contacts[i].hasOwnProperty(prop) == true) {
				console.log('contact (' +	name + ') has prop named ' +	prop + ' = ' + contacts[i][prop]);
				return contacts[i][prop];
			} else {
				console.log('contact (' + name + ') has no such property named ' + prop);
				return 'No such property';
			}
		} else {
			console.log(name + ' is not a valid contact');
			return 'No such contact';
		}
	}
	// Only change code above this line
}

#3

Thank you so much @randelldawson !
Yes, because of my return it couldn’t go further than the 1st contact ^^’
Also thank you for explaining how to debug :slight_smile: