Using while loop on a javascript prompt

 let welcome;
    welcome = "Welcome to my first COMP1231 Program.";
    alert(welcome)

    let name;
    name =(prompt("Please enter your name:?", "Tafadzwa Marisa" ));
    if(name===""){
        while(name===""){
            name =(prompt("Please enter your name:?", "Tafadzwa Marisa" ));
        }
    }
    let program;
    program =(prompt("Please enter your Program:", "COMP1231" ));
    if(program===""){
        while(program===""){
            program =(prompt("Please enter your Program:", "COMP1231" ));
        }
    }


    let year;
    year =parseInt(prompt("Please enter your year of study:", "1"));
    if(year ===""){
        while(year =1; year=>3 ""){
            year = parseInt(prompt("Please enter your year of study:", "1"));
        }
    }






So l am trying to use a prompt which asks a user for their year of study and when the user enters a correct input which is 1,2 or 3 it proceeds . But when the user doesn’t enter anything or enters any other number besides 1 or 2 or 3 it loops back to asks the user to enter their year of study. This Javascript

To me a while loop seems unnecessary. Why not use a select tag with 1,2, and 3 as option tags so you control the input in a modal rather than a prompt?

Or an input with type of number and set a min and max.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.