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

134
Vistas
Is there a way to use prompt and confirm box using javascript?

I'm new here and new to using javascript, I need help with creating a prompt that asks a user for their name and then displaying the name in a confirm box. If the name is correct it has to display a message that says "Hi" followed by their name. If its not correct it has to loop through and ask for name again. So I understand how to write a prompt and confirm box separately but I am struggling with putting the prompt and confirm together with a loop.

Here is what I have so far:

var name = prompt('What is your name?');
var confirm = confirm('Is your name: ' + name);

function myFunction() {
  var txt;

  if (confirm === true) {
    txt = "You pressed OK!";
  } else {
    txt = "Input your correct name";
  }
}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You basically just need to bootstrap myFunction, and also place the prompts inside the myFunction so they get called again if the confirmation is false.

eg.

function myFunction() {
  var name = prompt('What is your name?');
  var conf = confirm('Is your name: ' + name);
  if (conf === true) {
    alert("You pressed OK!");
  } else {
    alert("Input your correct name");
    myFunction();
  }
}

myFunction();

about 4 years ago · Juan Pablo Isaza Denunciar

0

I have encapsulated your code a function and call the function again if the confirmation is no.

Here, I am passing an extra parameter, which confirms if the questions are asked first time. If it not asked first time, then it will show Input your correct name. also.

function askAndConfirm(isFirst){
  var question="";
  if(!isFirst) question = "Input your correct name. ";
  var name = prompt(question + 'What is your name?');
  var isConfirm = confirm('Is your name: ' + name);

  if (isConfirm === true) {
    alert("Hi! "+name);
  } else {
    askAndConfirm(false);
  }
}
askAndConfirm(true);

about 4 years ago · Juan Pablo Isaza 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