Skip to content

Instantly share code, notes, and snippets.

@christurnertv
Created September 5, 2014 21:56
Show Gist options
  • Save christurnertv/df38f691135b68daeadb to your computer and use it in GitHub Desktop.
Save christurnertv/df38f691135b68daeadb to your computer and use it in GitHub Desktop.
<html>
<head>
<title>Contact Manager</title>
</head>
<body>
<button id="btnAddContact">Add Contact</button>
<script>
var contactManager = {
"contacts" : [
{
"first_name" : "Chris",
"last_initial" : "T"
},
{
"first_name" : "Ben",
"last_initial" : "B"
}
],
"printNames" : function () {
this.contacts.forEach(function (person, index) {
console.log(index + ': ' + person.first_name + ' ' + person.last_initial);
});
},
"addContact" : function (firstName, lastInitial) {
var contact = {
"first_name" : firstName,
"last_initial" : lastInitial
};
this.contacts.push(contact);
console.log('Added contact.');
}
};
var btnAddContact = document.getElementById('btnAddContact');
btnAddContact.addEventListener('click', function () {
var firstName = prompt('Contact First Name:');
var lastInitial = prompt('Contact Last Initial:');
contactManager.addContact(firstName, lastInitial);
console.log('====================');
contactManager.printNames();
});
console.log('====================');
contactManager.printNames();
</script>
</body>
</html>
<html>
<head>
<title>Contact Manager</title>
</head>
<body>
<button id="btnAddContact">Add Contact</button>
<script>
var contactManager = {};
contactManager.contacts = [
{
"first_name" : "Chris",
"last_initial" : "T"
},
{
"first_name" : "Ben",
"last_initial" : "B"
}
];
contactManager.printNames = function () {
this.contacts.forEach(function (person, index) {
console.log(index + ': ' + person.first_name + ' ' + person.last_initial);
});
};
contactManager.addContact = function (firstName, lastInitial) {
var contact = {
"first_name" : firstName,
"last_initial" : lastInitial
};
this.contacts.push(contact);
console.log('Added contact.');
};
var btnAddContact = document.getElementById('btnAddContact');
btnAddContact.addEventListener('click', function () {
var firstName = prompt('Contact First Name:');
var lastInitial = prompt('Contact Last Initial:');
contactManager.addContact(firstName, lastInitial);
console.log('====================');
contactManager.printNames();
});
console.log('====================');
contactManager.printNames();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment