Profile LookupIs: is this way correct?

Hello! I solved the exercise, this is my code:

    function lookUpProfile(name, prop){
        for (var i = 0; i < contacts.length; i++){
            if (contacts[i].firstName == name && contacts[i][prop] != undefined){
                return contacts[i][prop]
            }
        } 

        for (var i = 0; i < contacts.length; i++){
            if (contacts[i].firstName == name){
                return "No such property"
            }
        }

        return "No such contact"
    }

I am not sure if I’m following a good way, because I did two separate loops.

By the way, thanks

1 Like

Hey @nix!

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

I think this solution works well. I personally used a nested if statement inside my for loop.

You can always come back to these algorithms and refactor your solutions if you want when you have worked through the more advanced javascript sections.

I started refactoring my old solutions when I finished the javascript projects. There are a million different ways to solve the problem.

Great job!

Happy coding!

1 Like