How can I create a Javascript function for the prompt?

Asked

Viewed 230 times

-4

How can I create a function to let the prompt be provided with the 1 and 2 codes in javascript?

If it is not for either of these two continue at the same prompt. And if for continues the code ...

If so, can you leave the prompt of a function? for a function to be used in several different requests ...

Thank you very much

  • " to let the ready be provided" I don’t understand what that means

  • Important [Dit] the post explaining better what tried and what did not work, preferably with a [mcve] problem, so that a response is feasible. To better understand the site, do the [Tour] and make a toast in [Ask] and [Help].

  • I think that the question cannot be clearer, because it is a simple thing to do. I see that in most cases here in the community people may not know 100% the nomenclature of what they want, for example the boy, if he’s starting out in the area and needs help with something so simple, maybe he doesn’t even know what a while loop is. I believe it makes many people who come in here maybe not come back...

2 answers

0


Use a while to continue the function until a valid value is entered:

function validar() {
  var opcao = "0"
  
  while (opcao != "1" && opcao != "2") {
    opcao = prompt("Digite uma opção (1 ou 2)");
  }
  
  return opcao;
}


alert("Opção escolhida: " + validar());

  • 1

    I couldn’t understand the downvotes, which is so bad in the answer?

  • friend, thank you so much for the answer, by chance it would have like the function interact with a prompt that is outside of it?

  • yes, I could pass the value on to her: function validar(opcao) but it seems strange to me, I suggest creating another question explaining this so we can help ;)

  • https://answall.com/questions/401280/como-fa%C3%a7o-para-uma-Function-verificar-um-prompt-que-esta-fora-dela

  • I created another question, I would like to give an example there?

-3

Hello.

The function num() will continue when it is different from 1 ou 2.

When you go 1 ou 2,a message will appear on the screen.

Solution

function num() {
  var call = prompt();
  var ret = ["1","2","stop"];
  while (!ret.includes(call)) {
    call = prompt();
  }
  if (call == "stop") { return; } else {
  alert("Valor permitido")}
}
num();
  • 1

    Only one hint: use recursion (i.e., call the function num within itself) can cause a pile burst, even more in a case like this, where you do not control the output condition (because it depends on what is typed). Not to mention that if the user enters multiple invalid values, when typed 1 or 2, the Alert message will be printed several times. The best would be to use a loop even, as suggested by Ricardo’s reply (which has no stack burst, no matter how many invalid numbers are typed, and shows the message only once) :-)

  • To learn more about "recursion vs loops": https://answall.com/q/21551/112052 and https://stackoverflow.com/q/660337

  • @hkotsubo code worked,he wanted it repeated if it were different.

  • 1

    Another tip: as an experienced site user often says, "working is different from being right". Recursion, although "working", is not the most appropriate solution for this case, for the reasons already mentioned. By the way, I’m glad you edited the answer and withdrew the recursion. To better understand the difference, test the 2 codes (the previous and the current), typing several times the 5 and then the 2, and see the difference (the recursion calls the Alert several times at the end, the loop calls only one).

  • Sorry I forgot. I went to test gave endless loop.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.