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;
}
}
};
Aucun commentaire:
Enregistrer un commentaire