I am currently busy with a user list challenge and this is the challenge:
-add 3 new users to the database (get them via user input, not hard coding) -remove a user from the database by their index. (user input, no hard coding) -concatenate/merge all usernames in the database into one String and display it appropriately in -the DOM -sort the usernames in the database alphabetically and log it out to the console
This is my code:
let userlist = ["Steven", "Michael","Sarah","Lisa","Tamryn", "Wayne","Kirsten", "Peter", "Gwen", "Tamzin"];
console.log(userlist);
let menu = (prompt("=====Menu=====\n 1.Add three users\n 2.Remove User\n 3.Display All users\n 4.Sort users\n")-1);
switch(userlist)
{
case 0:
let newUsers = prompt("Add three user");
userlist.push(newUsers);
break;
case 1:
let removeUser = prompt("Remove a User");
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers,1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
let display = prompt("Display a User");
break;
case 3:
let sort = prompt("Sort users")
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
I am not sure on how to achieve the last two cases
I've gone ahead and corrected some of the errors in your program but there are still some in there in some of the functionality you have written that you need to correct. This is just to give you a working skeleton for your application.
Key changes:
number otherwise your switch won't work as you expect it tomenu not on userlist as userlist is an array and you are trying to compare that with a number in your switch statementYou may run the application now. Adding users as well as displaying users should work.
let userlist = [
"Steven",
"Michael",
"Sarah",
"Lisa",
"Tamryn",
"Wayne",
"Kirsten",
"Peter",
"Gwen",
"Tamzin",
];
console.log(userlist);
// make program run indefinitely (until tab/ browser is closed or Exit option is selected)
let exit = false;
while (true) {
let menu = prompt(
"=====Menu=====\n 1. Add an user\n 2. Remove user\n 3. Display all users\n 4. Sort users\n 5. Exit\n"
)
// Parse result as Integer
// you did it with -1, but it would be more natural to just parse the number as displayed on the screen and adjust the values in the switch accordingly
menu = Number.parseInt(menu) - 1;
if(isNaN(menu)){
// something else than a number was entered
alert("You must enter a number!")
// move on to next iteration => means here: display menu again.
continue;
}
// now use menu to determine which option was choosen
switch (menu) {
case 0:
// added an instruction so a user knows what to do
let newUsers = prompt("Add a user: \nPlease provide a username");
userlist.push(newUsers);
break;
case 1:
// added an instruction so a user knows what to do
let removeUser = prompt(
"==Remove a User==\nPlease provide the hame of the user you want to delete"
);
newUsers = userlist.indexOf(removeUser);
let removedUser = userlist.splice(newUsers, 1);
console.log(indexRemoved + " " + removedUser);
alert(`${removedUser} has been removed from the database`);
break;
case 2:
alert(`Display all users:\n ${userlist}`);
break;
case 3:
let sort = prompt("Sort users");
// todo
break;
case 4:
exit = true;
break;
default:
alert("Please select 1 - 4 to choose any of the options displayed.");
break;
}
// if user wants to exit the program break the infinite loop
if(exit){
exit = false;
break;
}
}
Your result is stocked in the menu variable, so your switch should compare this variable, not the userlist.
you just have to do the switch as it =>
switch(menu)