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
1

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.