Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>JS Bin</title>
- <style id="jsbin-css">
- h1 {
- color: red;
- font-family:Arial;
- }
- </style>
- </head>
- <body>
- <h1>Joe Bliss</h1>
- <script id="jsbin-javascript">
- var contacts = [
- {
- firstName : 'John',
- lastName : 'Doe',
- phone : '(512) 355-0453',
- email : 'johndoe@email.com'
- },
- {
- firstName : 'Jane',
- lastName : 'Doe',
- phone : '(313) 641-2203',
- email : 'janedoe@email.com'
- },
- {
- firstName : 'Suzie',
- lastName : 'Smith',
- phone : '(415) 604-4219',
- email : 'suziesmith@email.com'
- }
- ];
- var addContact = function (newContact) {
- contacts.push(newContact);
- return contacts;
- }
- addContact({
- firstName : 'Billy',
- lastName : 'Bob',
- phone : '(908) 328-2054',
- email : 'billybob@email.com'
- });
- console.log(contacts);
- var listContacts = function () {
- for (var i = 0; i < contacts.length; i++) {
- console.log(contacts[i].firstName + ' ' + contacts[i].lastName);
- }
- }
- listContacts();
- // Now let's create a function that will search through our contacts
- // and return the contact info for any person that has a matching first
- // or last name. Create a search function, which will take one parameter
- // , name. In the search function, use a loop to iterate through the
- // contacts array and return the current contact if their first or
- // last name matches the name parameter.
- var search = function (name) {
- for (var i = 0; i < contacts.length; i++) {
- if (name === contacts.firstName || name === contacts.lastName) {
- return contacts[i];
- }
- }
- };
- console.log(search(Jane));
- </script>
- <script id="jsbin-source-css" type="text/css">h1 {
- color: red;
- font-family:Arial;
- }</script>
- <script id="jsbin-source-javascript" type="text/javascript">var contacts = [
- {
- firstName : 'John',
- lastName : 'Doe',
- phone : '(512) 355-0453',
- email : 'johndoe@email.com'
- },
- {
- firstName : 'Jane',
- lastName : 'Doe',
- phone : '(313) 641-2203',
- email : 'janedoe@email.com'
- },
- {
- firstName : 'Suzie',
- lastName : 'Smith',
- phone : '(415) 604-4219',
- email : 'suziesmith@email.com'
- }
- ];
- var addContact = function (newContact) {
- contacts.push(newContact);
- return contacts;
- }
- addContact({
- firstName : 'Billy',
- lastName : 'Bob',
- phone : '(908) 328-2054',
- email : 'billybob@email.com'
- });
- console.log(contacts);
- var listContacts = function () {
- for (var i = 0; i < contacts.length; i++) {
- console.log(contacts[i].firstName + ' ' + contacts[i].lastName);
- }
- }
- listContacts();
- // Now let's create a function that will search through our contacts
- // and return the contact info for any person that has a matching first
- // or last name. Create a search function, which will take one parameter
- // , name. In the search function, use a loop to iterate through the
- // contacts array and return the current contact if their first or
- // last name matches the name parameter.
- var search = function (name) {
- for (var i = 0; i < contacts.length; i++) {
- if (name === contacts.firstName || name === contacts.lastName) {
- return contacts[i];
- }
- }
- };
- console.log(search(Jane));
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement