|
1 | | -var AddressBook = []; |
| 1 | +function AddressBook(){ |
| 2 | + var contactList = []; |
| 3 | + this.addContact = function(contact){ |
| 4 | + contactList.push(contact); |
| 5 | + }; |
| 6 | + this.deleteContact = function(contact){ |
| 7 | + var index = contactList.indexOf(contact); |
| 8 | + contactList.splice(index, 1); |
| 9 | + |
| 10 | + }; |
| 11 | + this.print = function(){ |
| 12 | + console.log(contactList); |
| 13 | + }; |
| 14 | + |
| 15 | + this.Find = function(Needed){ |
| 16 | + var answer = contactList.filter(function(x){ |
| 17 | + |
| 18 | + for(var field in x){ |
| 19 | + |
| 20 | + if(x[field] === Needed){ |
| 21 | + return x[field]; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + }); |
| 26 | + console.log(answer); |
| 27 | + }; |
| 28 | +} |
2 | 29 |
|
3 | 30 | function Contact(firstName, lastName, cellProvider, cellNumber){ |
4 | 31 | this.firstName = firstName; |
5 | 32 | this.lastName = lastName; |
6 | 33 | this.cellProvider = cellProvider; |
7 | | - this.cellNumber = cellNumber; |
8 | | - this.addToBook = function(){ |
9 | | - AddressBook.push(this); |
10 | | - }; |
11 | | - this.deleteFromBook = function(){ |
12 | | - var index = AddressBook.indexOf(this); |
13 | | - AddressBook.splice(index,index); |
14 | | - }; |
| 34 | + this.cellNumber = cellNumber; |
15 | 35 | } |
16 | | - |
17 | | -/* var filterednames = AddressBook.filter(function(obj) { |
18 | | - if (obj.firstName === "Ndabenhle"){ |
19 | | - console.log(obj.cellNumber); |
20 | | - } |
21 | | -}); |
22 | | - */ |
23 | | - |
24 | | - function findCellNumber(contactName){ |
25 | | - AddressBook.filter(function(obj){ |
26 | | - if(obj.firstName == contactName){ |
27 | | - return(obj.cellNumber); |
28 | | - } |
29 | | - } |
30 | | - ); |
31 | | - } |
32 | | - |
33 | | - |
34 | | -var nda = new Contact("Ndabenhle", "Ngulube", "Vodacom", "0798900606"); |
35 | | -var nhla = new Contact("Nhlanhla", "Ngulube", "MTN", "0798900607"); |
36 | | -var thomas = new Contact("Thomas","Razuko", "CellC", "0832546071"); |
37 | | -nda.addToBook(); |
38 | | -nhla.addToBook(); |
39 | | -thomas.addToBook(); |
40 | | -console.log(AddressBook); |
41 | | -console.log() |
42 | | -thomas.deleteFromBook(); |
43 | | -console.log(AddressBook); |
44 | | - |
45 | | -findCellNumber("Ndabenhle"); |
0 commit comments