1

Hi I'm looking for some advice on how I would make this switch statement into a do loop.both string function and number function execute other programs and have to be executed until 3 is pressed. any help would be greatly appreciated.

function Menu()
        {
            var menu =0;

            document.write(" menu options " + name + "<br>");
            document.write("option 1 stringFunction<br>");
            document.write("option 2 numberFunction<br>");
            document.write("option 3 goodbye<br>");

            menu = prompt("please select a number between 1 and 3",0);
            menu = parseInt(menu)

        switch (menu)
        { // begin switch

        case 1:
        // begin case 1
            document.write(name + " This is option 1<br> ") ;
            stringFunction()
        break ; 
        // end case 1                 

        case 2:
        // begin case 2
            document.write(name + " This is option 2<br>") ;
            numberFunction()
        break ;
    // end case 2

        case 3:
        // begin case 3
            document.write('Goodbye ' +  name) ;
        break ;
    // end case 3
        default :

        {// begin default
        alert ("You must choose either 1,2,or 3");
        }

    } // end switch

} // end function
aldo
  • 11
  • 3
  • This could be a solution for you : http://stackoverflow.com/questions/17072605/break-for-loop-from-inside-of-switch-case-in-javascript – Chaibi Alaa Nov 23 '15 at 17:59

1 Answers1

0

I would go about using a while or do while loop. In you condition check if inputed value is not 3.

//do while block

do{
   menu = prompt("please select a number between 1 and 3",0);
   menu = parseInt(menu)
   //more code
}while(menu!=3);

//Complete Code

 function Menu()
            {
                var menu =0;

                document.write(" menu options " + name + "<br>");
                document.write("option 1 stringFunction<br>");
                document.write("option 2 numberFunction<br>");
                document.write("option 3 goodbye<br>");

            do{
                menu = prompt("please select a number between 1 and 3",0);
                menu = parseInt(menu)


                switch (menu)
                { // begin switch

                case 1:
                // begin case 1
                    document.write(name + " This is option 1<br> ") ;
                    stringFunction()
                break ; 
                // end case 1                 

                case 2:
                // begin case 2
                    document.write(name + " This is option 2<br>") ;
                    numberFunction()
                break ;
            // end case 2

                case 3:
                // begin case 3
                    document.write('Goodbye ' +  name) ;
                break ;
            // end case 3
                default :

                {// begin default
                alert ("You must choose either 1,2,or 3");
                }

            }while(menu!=3);


        } // end switch

    } // end function
Nakib
  • 4,593
  • 7
  • 23
  • 45