简体   繁体   中英

Trouble with IF statement carrying variable value into switch

I'm trying to make a prompt where the user has to input a number within a range of 1 to 4 (re-prompted through a do-while loop until they input the correct number) and that number will then correspond to a case in the switch (which will open a new window). In testing, the code is correctly executed right up to my ELSE statement, where the code abruptly 'forgets' the number the user input to get out of the while loop and hence doesn't take the user to the code for each of the cases in the switch.

Testing : prompt = user inputs 5, new prompt: Invalid selection! please input a number between 1 and 4, user inputs 4, prompt closes.

Can anyone see why the reDirect variable in the IF statement doesn't make it into the switch?

I'll take any suggestions, but I need to give the user the 'freedom' to input numbers that are out of range to prove I can handle errors.

I tried implementing new variables in the if statement, but they too are left out of the switch which closes.

    function reDirect() {
       var reDirect = 0;
       reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
           if (reDirect >=5 || reDirect <=0) {
               var reDirect = 0
               do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
               while (reDirect >=5 || reDirect <=0);
           } 
           else
           {
           var element = document.getElementById("reDirect");
                 switch (reDirect) {
                      default:
                      break;    
                      case 1:
               window.open("URL","_blank","height=800px, width=800px");
                      break;
        }
    }

};

It sounds like you don't want to conditionally run the switch - you always want to run the switch , once you've verified that the input is valid.

Just move the switch out of the else , so that it'll run regardless:

function reDirect() {
  var reDirect = 0;
  reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
  if (reDirect >= 5 || reDirect <= 0) {
    do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
    while (reDirect >= 5 || reDirect <= 0);
  }
  var element = document.getElementById("reDirect");
  switch (reDirect) {
    case 1:
      window.open("URL", "_blank", "height=800px, width=800px");
      break;
    // other cases
    default: break; // ???
  }
}

Also note that function declaration blocks should not have ; s at the end, and that because reDirect is declared at the top of the function, you shouldn't try to re-declare it with var inside the if block. (You also might want to put in cases for cases 2-4, or put something more meaningful in the default case)

default comes at the end of the switch :

switch (reDirect) {
    case 1:
        window.open("URL","_blank","height=800px, width=800px");
        break;
    default:
        break;
}

Put the default at the end of the switch statement:

function reDirect() {
      var reDirect = 0;
      reDirect = parseInt(prompt('Input number between 1 and 4', '1'));
      if (reDirect >= 5 || reDirect <= 0) {
        var reDirect = 0;
        do
          reDirect = parseInt(prompt('Invalid! Input number between 1 and 4', '1'));
        while (reDirect >= 5 || reDirect <= 0);
      } else {
        var element = document.getElementById('reDirect');
        switch (reDirect) {
          case 1:
            window.open('URL', '_blank', 'height=800px, width=800px');
            break;
          default:
            break;
        }
      }
    }

Some changes:

  • You could change the loop for getting the value, to have only one part for it.

  • You need to move the default part at the end of the switch statement, because if this statement is found, this part is immediately used and the switch statements ends.

  • For parsing the number, you need a radix with parseInt .

  • To prevent using NaN , you need to switch the condition, which now covers a NaN value as well.

 function reDirect() { var reDirect; do { reDirect = parseInt(prompt("Input number between 1 and 4", "1"), 10); } while (!(reDirect >= 1 && reDirect <= 4)) //var element = document.getElementById("reDirect"); console.log(reDirect); switch (reDirect) { case 1: window.open("URL", "_blank", "height=800px, width=800px"); break; default: break; } } reDirect(); 

With in if statement you check if the reDirect is >=5||<=0 and make the do while loop without pass to what you have in else statement draw the window

function reDirect() {
   var reDirect = 0;
   reDirect = parseInt(prompt("Input number between 1 and 4", "1"));
       if (reDirect >=5 || reDirect <=0) {
           var reDirect = 0
           do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1"));
           while (reDirect >=5 || reDirect <=0);
draw();
       } 
       else
       {
draw();
}
}
function draw(){
       var element = document.getElementById("reDirect");
             switch (reDirect) {
                  default:
                  break;    
                  case 1:
           window.open("URL","_blank","height=800px, width=800px");
                  break;
}

Actually you have already defined the variable reDirect and Initialized it to 0. within if you are re declaring it.

 function reDirect() { var reDirect = 0; reDirect = parseInt(prompt("Input number between 1 and 4", "1")); if (reDirect >=5 || reDirect <=0) { //var reDirect = 0 reDirect = 0 do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1")); while (reDirect >=5 || reDirect <=0); } else { var element = document.getElementById("reDirect"); switch (reDirect) { default: break; case 1: window.open("URL","_blank","height=800px, width=800px"); break; } } 

Your code works fine just after removing the "var reDirect"

 function reDirect() { var reDirect = 0; reDirect = parseInt(prompt("Input number between 1 and 4", "1")); if (reDirect >=5 || reDirect <=0) { reDirect = 0 do reDirect = parseInt(prompt("Invalid! Input number between 1 and 4", "1")); while (reDirect >=5 || reDirect <=0); } else { var element = document.getElementById("reDirect"); switch (reDirect) { default: break; case 1: console.log(1) window.open("URL","_blank","height=800px, width=800px"); break; } } } reDirect() 
 <p id="reDirect"> what </p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM