var userJohn = new Member ('John', '20');
var userKelly = new Member ('Kelly', '24');
class Member {
constructor (name, age) {
this.name = name;
this.age = age;
}
}
function subMemberGroups() {
for (var i = 0; i < memberGroup.length; i++) {
subMemberGroup.push(memberGroup[i]["name"]);
}
}
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 if (memberName.search(/^[a-zA-Z\s]+$/) == -1) {
console.log ("Invalid Name!");
}
else if (memberName.search(/^[a-zA-Z\s]+$/) == 0) {
var memberAge = input.question("Please Age: ");
var userNew = new Member (memberName, memberAge);
memberGroup.push(userNew);
break;
}
}
What the above codes are doing is asking user to input a name. The function subMemberGroups will then check if the name that the user has input exists in the array. If John is input by the user, then it will be invalid after the function has check. After which, if all else is good i.e the name doesn't exist and if the name as no number or special characters, it will then be pushed to a new array called userNew.
What I am trying to achieve here is to permanently add the new user to the array without the data being lost after exiting the program. I am currently using Visual Studio Code and whenever I ctrl C to exit, all the existing data is lost i.e the new user I added. Is there any solution to this?