I do not know the reason why my code is not working
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0; i < contacts.length; i++) {
//console.log(contacts[i]);
if(name=="first name"&&prop==contacts[i]){
return prop;
if (name!==prop) {
return "No such contact";
if (prop!==contacts[i])
return "No such property";
}
}
}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
all your if statements are nested in each other. this is your first biggest logical error
if this is true: name=="first name"&&prop==contacts[i]
this is what is executed:
most of your code is executed only if that first condition is true
plus, the first line is a return statement, which stops the function, so everything after is not executed
after this, you are also checking the wrong things name !== prop is always true, as one is the name of the contact and the other is the name of the property you need to give the value of prop !== contacts[i] the first is a string the second an object, this comparison doesn’t have any sense
( also name=="first name" where name will never be the string "first name", you may want to review how to access object properties)
there are also a few other things, but if you fix these is a good start
I am trying to build the first condition, but I do not know how to iterate in specific parts of contacts. What may I do?
function lookUpProfile(name, prop){
// Only change code below this line
if( name == "firstName" && prop=="contacts"){
return value;
}
// if (firstName!==) {
return "No such contact";
//}
if (prop!==){
//return "No such property";
//}
// Only change code above this line
}
// Change these values to test your function
lookUpProfile("Akira", "likes");
Remember that as soon as JavaScript sees a return statement, the value specified is return and the function is exited. Based on the way you have written your code, in the first iteration of the contacts array, you are returning a value and then the function exits and stops iterating. Think about when you really should return certain values.
contacts is an array of objects, it will never be equal to a variable that has a value of string
you may want to review how to access array elements and object properties.
You have a few issues, but let’s handle the one below first.
contacts[i] is an object, so prop (which is a string) will never equal to contacts[i], so this statement will always evaluate to false. What are you trying to check in this first if statement? Also, what are you trying to return in the following line?
return contacts[i];
I ask because currently you are just returning an object.
here you are checking if the prop variable, which would be a property name, and the value of a property are equal
also, be careful with this one:
what happens if name is "Sherlock"?
when you have "Sherlock" !== "Akira" this will execute, as that is the value of contacts[0]["firstName"], and they are
not equal to each other
in this case it’s more a matter of algorithm solving
you need to reason and find a different way
I will tell you that if you put a return statement inside a loop and the return statement always execute it is like you don’t have a loop as it will stop at first iteration
try looking at your code with this tool, look at the tests that pass then one by one the tests that do not pass and try to find out how the things need to be changed http://www.pythontutor.com/javascript.html
You need to review the difference between assignment operator and comparison operator - here can lay all the difference between working and not working
If you have made so that inside a loop a return statement will always execute (like it happens when you have an else statement) it is like the loop doesn’t exist because at first iteration one of the return statements is executed, the function is exited having an output and the loop stops at first iteration
prop==contacts[i][prop] this is comparing the value of a certain prop with the name of a certain prop - could they be equal?