lundi 29 mai 2017

Using an else-if statement with in a try-catch-finally

I am trying to use a try-catch-finally statement to error check user input and then advance to the next page if there are no error (I know there's easier ways to do this but I'm being required to use a try-catch-finally). In this case: foo is the user's input, the input needs to be a number between 0 and 100, and displayDiagram() will advance the user to the next page.

Everything works for me up until the finally block. If the user does not enter anything at all, it advances to the next slide. How do I stop it from advancing if nothing has been entered?

Here's the code:

 try {
      if (foo == "") throw "Enter your estimated score";
      if (isNaN(foo)) throw "Only use numbers for your estimated score";
      if (foo < 0) throw "Estimated score is not valid. Enter a higher number";
      if (foo > 100) throw "Estimated score is too high. Enter a lower number";

  }
  catch(err) {
    document.getElementById("fooMessage").innerHTML = err;
    document.getElementById("fooInput").style.borderColor = "red";

  }
  finally {
    if (err = undefined) {
     displayDiagram();
   }
 }

Another finally block I have tried includes:

finally {
        if (preScore >= 0 && preScore <= 100) {
         displayDiagram();
       } else if (typeof preScore != "string") {
         displayDiagram();
        }
      }

Any ideas? Thanks!

Aucun commentaire:

Enregistrer un commentaire