How to make after running Alert(e), go back to the first line of Code

Asked

Viewed 116 times

-3

I wanted to make sure after the alert error, the code returned to the first line, in case: var p=parseFloat(prompt("Digite um numero"));.

I tried to put one return after the alert, but it didn’t work. Someone knows how to do?

var p=parseFloat(prompt("Digite um numero"));
try{
    if(isNaN(numbers)){
        throw new Error();
    }
   escrever(p);
}catch(e){
    alert(e);
}

function escrever(numbers){
    const doc=document.querySelector("#seletor");
    doc.innerHTML=numbers;
}
  • Javascript (fortunately? :P) does not have goto explicit. One way to do this is with while, but the logic is a little different. :)

  • @Luizfelipe did not understand, could explain better or make a code showing an example, please?

  • Try to improve your question first. : ) For example, instead of putting code per image, put it in text. Also try to describe the problem a little better.

  • @Luizfelipe formulated better

3 answers

4

If you want something to happen over and over again, use a loop - That’s what they’re for :-)

Besides, it makes no sense to launch a Error inside the block try just to catch it right away. And the Error that you have created neither message, so nothing useful will be shown (only "Error").

You don’t need Error nor of try/catch. If you want an error message to be shown if the result is NaN, place it inside the if even:

function escrever(numbers) {
    const doc = document.querySelector("#seletor");
    doc.innerHTML = numbers;
}

while (true) {
    var p = parseFloat(prompt("Digite um número"));
    if (isNaN(p)) {
        alert('Não foi digitado um número');
    } else {
        escrever(p);
        break; // sai do while
    }
}
<p id="seletor"></p>

while (true) creates a loop "infinite" - it repeats itself until it finds some break, and in that case the break is only called when a valid number is entered (but if you want this to keep repeating itself indefinitely, simply remove the break).


To another answer suggested using recursion (i.e., creating a function and calling it within itself), but this is not a good idea, because each recursive call will occupying space in the execution stack, and this can burst the stack if it occurs many times. Even if it does not burst, it makes no sense to keep calling several times the same function within itself, when a simple loop resolves.

Another detail is that in the same answer, the function escrever is created within the function setNumber, and this causes a new function to be created every time setNumber is called. That is, in addition to improperly using recursion (occupying the execution stack without need), it still creates several functions for nothing. In short, don’t do it :-) Use the loop above which is already sufficient.

To learn more about recursion (what it is, and especially when not using it), read here, here, here, here and here.

-1

One of the alternatives is to brush the prompt call into a function (can even in a function variable), to be reused every time it falls into the catch. Try, for example:

openPrompt("Digite um numero");
var p;

try {
  if (isNaN(numbers)) throw new Error();
  escrever(p);

} catch(e) {
  alert(e);
  openPrompt("Digite um numero");
}

function escrever(numbers) {
  const doc = document.querySelector("#seletor");
  doc.innerHTML = numbers;
}

function openPrompt(message) {
  return p = parseFloat(prompt(message));
}

-1

You can use a loop do-while and a variable to indicate whether the loop should repeat or close (repetirLoop).

var repetirLoop;
do {
    repetirLoop = false;
    var p=parseFloat(prompt("Digite um numero"));
    try{
        if(isNaN(numbers)){
            throw new Error();
        }
    escrever(p);
    }catch(e){
        alert(e);
        repetirLoop = true;
    }

    function escrever(numbers){
        const doc=document.querySelector("#seletor");
        doc.innerHTML=numbers;
    }
}
while (repetirLoop);

Browser other questions tagged

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