var memberName = input.question("Please enter member's name: ");
memberName = memberName.substring(0, 1).toUpperCase() memberName.substring(1).toLowerCase();
subMemberGroups();
if (subMemberGroup.includes(memberName)) {
const memberGroupIndex = memberGroup.findIndex((e) => e.name === memberName)
if (memberGroupIndex !== -1) {
memberGroup.splice(memberGroupIndex, 1); // if exists remove it
console.log("Member deleted!\n");
} else {
console.log("Member does not exist.\n");
}
}
else {
console.log("Member does not exist.\n");
}
choices();
while (true) {
var memberName = input.question("Please enter member's name: ");
memberName = memberName.substring(0, 1).toUpperCase() memberName.substring(1).toLowerCase();
subMemberGroups();
if (subMemberGroup.includes(memberName)) {
console.log ("\nMember's name exists in database. Please enter a new name.");
}
else {
var memberDOB = input.question("Please enter member's date of birth: ");
var memberDJ = input.question("Please enter member's date joined: ");
var userNew = new Member (memberName, 'Ruby', memberDJ, memberDOB, 0);
memberGroup.push(userNew);
break;
}
}
while (true) {
var memberName = input.question("Please enter member's name: ");
memberName = memberName.substring(0, 1).toUpperCase() memberName.substring(1).toLowerCase();
subMemberGroups();
if (subMemberGroup.includes(memberName)) {
console.log ("\nMember's name exists in database. Please enter a new name.");
}
function subMemberGroups() {
for (var i = 0; i < memberGroup.length; i ) {
subMemberGroup.push(memberGroup[i]["name"]);
}
}
memberGroup = [userJohn, userLemuel];
subMemberGroup = ['John', 'Samuel'];
var userJohn = ['John', '20'];
var userLemuel = ['Lemuel', '22'];
var userJohn = new Member ['John', '24'];
The first block of code will delete the details of either of them depending on what the user input. The code works fine. However, after trying to add new member, it prompts me that the member already exist. How do I permanently delete the user details in the 2D array?
The second block of code will add new member. However, after the first block of code executes, the second block of code says that the name already existing although I have already ran the first block od code to delete the member i.e John.
I was wondering what can I do to resolve this?
CodePudding user response:
When you remove a member, you remove him from memberGroup
but he is still in subMemberGroup
, when you try to add / remove a member, you first check inside subMemberGroup
if he is already existing
Try edit like this :
if (subMemberGroup.includes(memberName)) {
const memberGroupIndex = memberGroup.findIndex((e) => e.name === memberName)
if (memberGroupIndex !== -1) {
memberGroup.splice(memberGroupIndex, 1); // if exists remove it
subMemberGroup.splice(subMemberGroup.indexOf(memberName), 1); // add this line to remove member from subMemberGroup
console.log("Member deleted!\n");
} else {
console.log("Member does not exist.\n");
}
}