JavaScript: Profile Lookup


//Approach 2
// if our iteration numbers are above the list
// end it; 
// else, determine next match
// if matched, end it
// else increase iteration, and search again

var propertyRequest
var searchPosition = 0;

function lookUpProfile(name, prop){
    if (searchPosition >= contacts.length){
        return "No such contact";
    } else if (name == contacts[searchPosition].firstName){
        if (propertyRequest = contacts[searchPosition][prop]){
            return propertyRequest;
        } else {
            return "No such property"
    } else {
        console.log("incremented to: " + searchPosition);
        return lookUpProfile(name, prop);

lookUpProfile("Akira", "likes");

Solved it! Some things I tried differently:

  • Reversed the sequence to define the stopping point at the beginning of the function rather than at the end. This is important because the function is recursive at the end. So you have to define the stopping point before it goes into a recursive loop.
  • Debugged by using true and false booleans to manually trigger the if-else statements to make sure that each part was working. I was able to see that it was skipping the else-if.
  • Another exercise would be to re-write the solution with a for-loop and see whether it'd be viable.


Working through this problem, to look up a name and iterate through the list.

  • Tried to use a for loop, but couldn't tell when the list didn't match the list
  • Maybe I should use a case or recursive functions
  • I can match the name, but I can't get the function to know that the name isn't on the list. Stuck on tracking which item we've been iterating through.
// Approach 1
// check name against list
// if name exists, keep going
// if it doesn't go to the next one
// if you've reached the end, say this person doesn't exist

// 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){
var firstName
var propertyRequest

matchName(name, prop)

    function matchName(name, prop){
        var searchPosition = 0;        
        if (searchPosition > contacts.length){
            console.log("No such contact");
            return "No such contact"
        } else {
            if (firstName = contacts[searchPosition].firstName){
                console.log("Found " + firstName);
                return "Found" + firstName;
            } else {
                matchName(name, prop);

// for (var i = 0; i < contacts.length; i++){
//     firstName = contacts[i].firstName
//     if (firstName == name){
//         console.log(i + ": "+ firstName)
//     } else {
//         console.log("not" + i + "person")
//         return "not here"
//     }
// }

lookUpProfile("Akira", "likes");
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.