Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

291
Vistas
how to display user input in a switch statement?

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

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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:

  • You need to parse the input as number otherwise your switch won't work as you expect it to
  • You need to switch on the parsed value of menu not on userlist as userlist is an array and you are trying to compare that with a number in your switch statement
  • I've added an infinite loop to your application which means a user will always go back to the menu after some option has been selected and can select the next thing he/ she wants to do. To be able to exit this loop I have added an Exit options which will end the application.

You 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;
  }
}

over 4 years ago · Santiago Trujillo Denunciar

0

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)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda