const blockedList = [];
function
blockUser() {
const userInput =
document.getElementById(
'userInput'
);
const userName = userInput.value.trim();
if
(userName !==
''
&& !blockedList.includes(userName)) {
blockedList.push(userName);
displayBlockedList();
}
else
if
(blockedList.includes(userName)){
alert(
"User already Blocked!"
)
}
else
if
(userName ==
''
){
alert(
"Please enter a username!"
)
}
userInput.value =
''
;
}
function
displayBlockedList() {
const listContainer =
document.getElementById(
'blockedList'
);
listContainer.innerHTML =
''
;
blockedList.forEach(user => {
const listItem = document.createElement(
'li'
);
listItem.textContent = user;
listContainer.appendChild(listItem);
});
}
function
unblockUser(index) {
blockedList.splice(index, 1);
displayBlockedList();
}
function
displayBlockedList() {
const listContainer =
document.getElementById(
'blockedList'
);
listContainer.innerHTML =
''
;
blockedList.forEach((user, index) => {
const listItem = document.createElement(
'li'
);
listItem.textContent = user;
listItem.className =
'blockeduser'
;
const unblockButton =
document.createElement(
'button'
);
unblockButton.textContent =
'Unblock'
;
unblockButton.className =
'unblockbutton'
;
unblockButton.onclick =
function
() {
unblockUser(index);
};
listItem.appendChild(unblockButton);
listContainer.appendChild(listItem);
});
}