Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

126
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda