FCC - Basic: Profile Lookup

my solution to the FCC Basic js Profile Lookup, with added results area and button to run the function.

Demo


//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){
// Only change code below this line

  var result_html = document.getElementById("result");
  // result_html.innerHTML = out;

  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
        if (contacts[i].hasOwnProperty(prop)) {
            result_html.innerHTML = contacts[i][prop];
            return contacts[i][prop];
        } else {
            var result_text = "No such property";
            result_html.innerHTML = result_text;
            return "No such property";
        }
    }
  }
  var result_text = "No such contact";
  result_html.innerHTML = result_text;
  return result_text;

  // Only change code above this line
}

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

HTML

<div id='result' class='mb-3 alert alert-primary'>..</div>

<button onclick='lookUpProfile("Akira", "likes");' class='btn btn-primary mb-1'>lookUpProfile("Akira", "likes")</button>

<button onclick='lookUpProfile("Harry", "likes");' class='btn btn-primary mb-1'>Harry Likes</button>

<button onclick='lookUpProfile("Sherlock", "likes");' class='btn btn-primary mb-1'>Sherlock Likes</button>

<button onclick='lookUpProfile("Kristian", "likes");' class='btn btn-primary mb-1'>Kristian Likes</button>

<button onclick='lookUpProfile("Bob", "likes");' class='btn btn-primary mb-1'>Invalid (Bob) Contact</button>

Result

..

Other Items in js

Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹