JavaScript: profile lookup Nightmare

JavaScript: profile lookup Nightmare
0.0 0

#1

So i have been working on this like 2 days straight (it usually takes me very little time to complete a task) at the beggining itlook very simple i start doing it and geting error and errors they was something i could understand and i didnt know what i read every single post about this class, i continue to have errors i give up after almost brooking my keyboard (it doenst work properly i cannot use ctrl z or anything like that)
i decided to sleep on it, today i wake up full of hope and energy i undertand know more properly all of the task fix almost all of my errors but know im stuck again and i simply cannot find what is wrong with my code.

please look it out and tellme what is it, maybe its something really dumb likea coma or something like that.

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

// running test
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”,“likes” should return an array
// tests completed

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


#2

sorry for my english btw


#3

Your for loop only iterates one time, because in your first if statement if the current object’s firstName property does not equal the name argument passed into the function, your else statement returns “No such contact”.

Remember, once a return statement as been executed, the function exits immediately.

Hint: You do not want to return No such contact until you have checked all of the objects’ firstName values. When is that complete?


#4

Omg that makes total sense i just take out the return out of my loop and it work perfectly, i tried something kind of like that early this morning but what i tried was a for else and it didnt work, thank u so much for your help <3