Question

This question relates to Javascript. What is the simplest way to modify the code below so...

This question relates to Javascript.

What is the simplest way to modify the code below so that it keeps asking for the user to enter the correct value if the user enters an incorrect response. At the moment the code willaccept empty that the user enters nothing. Please show me how to fix this. Please keep it simple because I am new to coding.

       const VALID_TEAMS = ["Australia","New Zealand","South-Africa","Pakistan","Brazil","West Indies"];
       const MIN = 0;
       const MAX = 30;
       const TEAM_NUM = 6;
              
       var score = 0;
       var findTeam;
       var team;
       var i;
      
       document.writeln("");
      
       for(i=0;i<=TEAM_NUM;i++){
           team = prompt("enter the team");
           findTeam = VALID_TEAMS.indexOf(team);
               if(findTeam>=0 && findTeam<=5){
                   document.writeln("");
                   document.writeln("");
                   score = prompt("enter the score");
                   while(isNaN(score) || score<0 || score>30 || score %3 !=0){
                       score = prompt("enter the score");
                   }
                       document.writeln("");  
                  
                  
               }
       document.writeln("");  
       }
       document.writeln("

"+team+" "+score+"

");
      
      

0 0
Add a comment Improve this question Transcribed image text
Answer #1

instead of using for loop you should use while loop and add a condition to handle the case of incorrect team input to prompt user again to enter the team name.

below is the modified code :

const VALID_TEAMS = ["Australia","New Zealand","South-Africa","Pakistan","Brazil","West Indies"];

       const MIN = 0;

       const MAX = 30;

       const TEAM_NUM = 6;

              

       var score = 0;

       var findTeam;

       var team;

       var i=0;

      

       document.writeln("");

    //   changed for loop to while loop

       while(i<TEAM_NUM){

           team = prompt("enter the team");

           findTeam = VALID_TEAMS.indexOf(team);

               if(findTeam>=0 && findTeam<=5){

                   document.writeln("");

                   document.writeln("");

                   score = prompt("enter the score");

                   while(isNaN(score) || score<0 || score>30 || score %3 !=0){

                       score = prompt("enter the score");

                   }

                       document.writeln("");  

                  

               }

            //    added else statement for the case of incorrect input

               else{

                team = prompt("enter the team");

               }

            i++;

       document.writeln("");  

       }

       document.writeln("");

here are some images:

if this answer helped you then please upvote and feel free to comment any queries.

thankyou !

Add a comment
Know the answer?
Add Answer to:
This question relates to Javascript. What is the simplest way to modify the code below so...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT