Can't understand why my codes ain't running, can you help me?

Tell us what’s happening:
I runned these codes a couple of times not running

Your code so far

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){
        return contacts[i][prop];
        return "No such property";
    return "No such contact";
// Only change code above this line

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

Your browser information:

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

Challenge: Profile Lookup

Link to the challenge:

Hi and welcome to the forum :slight_smile: You’re off to a good start with this task, but there are two things you need to be careful about:

  1. When you define the parameters for your for loop, you’ve put the condition to be i <= contacts.length, but this means that the loop will run even when the index is equal to the length of the list, so if it ever reaches that last iteration, the index will be out of range.
  2. Take a look at the structure of your for loop. You have a couple of conditionals, but whatever happens, you’re returning a value. And remember that your function ends immediately when it sees the return statement. This means that it’s returning something in the first iteration, so it only ever gets a chance to check the first object in the list, and never anything else.

I hope this helps! See if you can figure out how to fix these issues, and if you get stuck, we’re here to help you with some more hints :smile:

Thank you, there are a couple of things that I was not caring about. But thank you got to see how to fix them.

In case you cannot figure it out after that previous reply, I ll leave some hints that can help you solve the problem. It s just one approach. Notice that I used spoiler so in case you don’t want the answer at any cost, better don’t check it out.

Since you check for conditions and you have to return something clearly the function will stop before it gets to check the rest of the array. One thing you can do is, outside the for loop but inside the function you can declare some variables to keep track whether you’ve found a contact and/or a property, after you have looped through the entire array.

You can even try assigning some Boolean values to those variables so that after the for loop is finished you can check for those values of the variables and return which value wasn’t found. Now inside the loop if you find both, the name and the prop you can safely return but before returning you can also reassign the values for the variables declared outside the loop so if the loop finished and didn t find either of those, the outside variables know that.

I tried creating a variable, which I assigned to the prop values for the return but, the variable is getting the string variable only without acceptingCapture the array being assigned to it in other option.

If you ve found both, then you need to return the value of the prop. Now you need to also return something if you didn t find either a contact or a prop for that object(so you kinda need to keep track of both to see which one wasn’t found: contact or the prop), but you cannot return inside the for loop since it will end it before it gets to iterate over everything. Also you need to change i <= contacts.length.

yes I changed the i <= contacts.length to be i <= contacts.length - 1 I tried to keep track and I found that when we pass the function the key for property with one value it displays every thing well, but for the property keys with values which are arrays(such as likes) there is an error. can you help with that information.

Thank you finnaly figured out how @kristina_v, @Osiris

1 Like

Did you finish the challenge?

Yes I did a couple of hours ago

But I did shifted to another logic whic seem to be quite a good aproach toward the challenge.