-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbt7.js
106 lines (97 loc) · 2.77 KB
/
bt7.js
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// C/R/U/D - phone list
// Cho user input C/R/U/D
// C - name,phone -> them vao mang
// => R
// R - in ra toan bo contact theo name-phone
// U - cho user tim contact theo name
// ko thay thi not found
// thay thi update ten va sdt moi
// (ten moi ko trung ten cu)
// => R
// D - cho user tim contact theo name
// ko thay thi not found
// thay thi xoa contact
// => R
// ****Neu co roi ko tao moi
// ****Tim ten rut gon
const phonelist = [
{
name: 'phong',
phone: '01658214578',
},
{
name: 'phu',
phone: '01010203232',
}
];
function findname(arrinput) {
let LastNameArray = [];
for (let i = 0; i < phonelist.length; i++) {
x = phonelist[i].name.split(' ');
LastNameArray.push(x[x.length - 1]);
}
InputtoArray = arrinput.split(' ');
LastNameInput = InputtoArray[InputtoArray.length - 1];
for (let i = 0; i < LastNameArray.length; i++) {
if (LastNameArray[i] == LastNameInput) {
find = i;
break;
} else {
find = -1;
}
} return find;
};
function showall(arr) {
for (contact of arr) {
console.log(contact.name + ' - ' + contact.phone);
};
};
while (true) {
let ui = prompt('Nhap thao tac can lam C/R/U/D ?').toUpperCase();
if (ui == 'R') {
showall(phonelist);
}
else if (ui == 'C') {
let newname = prompt('Nhap ten lien lac moi');
findname(newname);
if (find == -1) {
let newphone = prompt('Nhap so');
phonelist.push({ name: newname, phone: newphone });
showall(phonelist);
} else {
alert('Ten nay da co roi');
};
}
else if (ui == 'U') {
let uiu = prompt('Ten lien lac can sua?');
findname(uiu);
let FixIndex = find;
if (find == -1) {
alert('Khong tim thay ten');
} else {
let newname1 = prompt('Nhap ten moi');
findname(newname1);
if (find == -1) {
let newphone1 = prompt('Nhap so moi');
phonelist[FixIndex].name = newname1;
phonelist[FixIndex].phone = newphone1;
showall(phonelist);
} else {
alert('Ten nay da co roi');
}
};
}
else if (ui == 'D') {
let uid = prompt('Ten lien lac muon xoa?');
findname(uid);
let DeleteIndex = find;
if (find == -1) {
alert('Khong tim thay ten');
} else {
phonelist.splice(DeleteIndex, 1);
showall(phonelist);
};
} else {
alert('Ban nhap sai cau lenh');
};
};