-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonebooktask.js
More file actions
79 lines (76 loc) · 2.42 KB
/
phonebooktask.js
File metadata and controls
79 lines (76 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var ContactInfo = function () {
this.name = name;
this.phone = phone;
this.email = email;
};
var Phonebook = function () {
this.phonebook = [];
function phoneValidation(inputtxt) { // xx-xxx-xxxxx
return /^[0-9]{2}-[0-9]{3}-[0-9]{4}$/.test(inputtxt);
}
Phonebook.prototype.add = function (contactInfo) {
if (contactInfo.name.length > 100) {
console.log("The Maximum name length is 100 characters");
return;
} else if (!phoneValidation(contactInfo.phone)) {
console.log("Enter a valid phone number");
return;
} else if (this.phonebook.length >= 10000) {
console.log("You reached the maximum contacts numbers");
return;
}
this.phonebook.push(contactInfo);
};
Phonebook.prototype.remove = function (index) {
if (index && this.phonebook[index]) {
this.phonebook.splice(index, 1);
}
};
Phonebook.prototype.search = function (query) {
var tempArray = this.phonebook.filter(function (contact) {
return contact.name.includes(query);
});
tempArray.push.apply(
tempArray,
this.phonebook.filter(function (contact) {
if (tempArray.indexOf(contact) == -1) {
return contact.phone.includes(query);
}
})
);
return tempArray && tempArray.length > 0 ? tempArray : [];
};
Phonebook.prototype.list = function (contactsPerPage, page) {
if (contactsPerPage) {
var startPoint = contactsPerPage * page;
var listPhonebook = [];
if (this.phonebook.length > startPoint) {
for (var phoneIterator = startPoint; phoneIterator < startPoint + contactsPerPage; phoneIterator++) {
listPhonebook.push(this.phonebook[phoneIterator]);
}
return listPhonebook;
}
} else {
return this.phonebook;
}
};
};
function pad(num, size) { // helper for testing 0001 instead of 1
var s = num + "";
while (s.length < size) s = "0" + s;
return s;
}
var tester = function () { // main testing function
var myPhoneBook = new Phonebook();
for (var phoneIterate = 0; phoneIterate < 9999; phoneIterate++) {
myPhoneBook.add({
name: "John Smith9",
phone: "02-234-" + pad(phoneIterate, 4),
email: "j.smith@mail.com"
});
}
console.log(myPhoneBook.list(10, 5));
myPhoneBook.remove(2);
console.log(myPhoneBook.search("9"));
};
var testInst = new tester(); // begin testing