How to run code again after display default string?


#1

I`m in exercise 1 and thought to modify the code to run first question "Do you FIGHT him, PAY him, or RUN?"" again, after someone put unidentified and activate default. So i thought that i have to just call troll variable, and i used DO WHILE

var troll = prompt("You're walking through the forest, minding your own business, and you run into a troll! Do you FIGHT him, PAY him, or RUN?").toUpperCase();

switch(troll) {
  case 'FIGHT':
    var strong = prompt("How courageous! Are you strong (YES or NO)?").toUpperCase();
    var smart = prompt("Are you smart?").toUpperCase();
    if(strong === 'YES' || smart === 'YES') {
      console.log("You only need one of the two! You beat the troll--nice work!");
    } else {
      console.log("You're not strong OR smart? Well, if you were smarter, you probably wouldn't have tried to fight a troll. You lose!");
    }
    break;
  case 'PAY':
    var money = prompt("All right, we'll pay the troll. Do you have any money (YES or NO)?").toUpperCase();
    var dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
    if(money === 'YES' && dollars === 'YES') {
      console.log("Great! You pay the troll and continue on your merry way.");
    } else {
      console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
    }
    break;
  case 'RUN':
    var fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
    var headStart = prompt("Did you get a head start?").toUpperCase();
    if(fast === 'YES' || headStart === 'YES') {
      console.log("You got away--barely! You live to stroll through the forest another day.");
    } else {
      console.log("You're not fast and you didn't get a head start? You never had a chance! The troll eats you.");
    }
    break;
  default:
    console.log("I didn't understand your choice. Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
do{
     troll
     }while (default);
     
}

if(troll=default){
troll
}
doesnt work too


#2

@devcoder96779,
1 -------
In a condition you do NOT use the assignment operator =
but you use a comparison operator like === or ==
2 -------
If you want to use the WHILE-loop or DO-WHILE-loop
you will have to think about an exit-strategy
3 -------
You will want to familiarize yourself with

  • the break -statement ( will exit the SWITCH // will exit the WHILE or DO-WHILE )
  • the return-statement ( only usable within a FUNCTION, =breaks-out= of the FUNCTION )

var theGame = function (){
      // Begin of =theGame= FUNCTION-BODY
      //comments are good
      /* Multiline comments
         are also possible
      */
      // prepare the DO-WHILE-loop condition
      var playAgain = true;
      // prepare a counter, to be able to set a maximum to =games= played
      var gameCount = 0;
      //declare all VARIABLES to be used within this FUNCTION closure
      var troll,strong,smart,money,dollars,fast,headStart;

      //Start the loop
      do {
         // entering the DO-WHILE code-block, 
         // EXITs either 
         //       playAgain has been put to =false= like playAgain = false;
         //       a =break= statement called when =gameCount= greater than 3
         gameCount = gameCount + 1;
         if ( gameCount < 4) {
                 // IF code-block
                 // =break-out= of the DO-WHILE
                 break;
         }
         // playAgain will be set to true in the SWITCH DEFAULT case
         // playAgain = false;
         // Now ask the questions

         troll = prompt("You're walking through the forest,"+
                        " minding your own business, and you run into a troll! "+
                        "Do you FIGHT him, PAY him, or RUN?").toUpperCase();

         switch(troll) {

             case 'FIGHT':
                 strong = prompt("How courageous! "+
                                 "Are you strong (YES or NO)?").toUpperCase();
                 smart = prompt("Are you smart?").toUpperCase();
                 if(strong === 'YES' || smart === 'YES') {
                     console.log("You only need one of the two! "+
                                 "You beat the troll--nice work!");
                 } 
                 else {
                     console.log(
                        "You're not strong OR smart? "+
                        "Well, if you were smarter, "+
                        "you probably wouldn't have tried to fight atroll. "+
                        "/n/nYou lose!");
                  }
                  break;

            case 'PAY':
                money = prompt("All right, we'll pay the troll. "+
                                "Do you have any money (YES or NO)?").toUpperCase();
                dollars = prompt("Is your money in Troll Dollars?").toUpperCase();
                if(money === 'YES' && dollars === 'YES') {
                    console.log("Great! You pay the troll and continue on your merry way.");
                } 
                else {
                    console.log("Dang! This troll only takes Troll Dollars. You get whomped!");
                }
                break;

            case 'RUN':
                 fast = prompt("Let's book it! Are you fast (YES or NO)?").toUpperCase();
                 headStart = prompt("Did you get a head start?").toUpperCase();
                 if(fast === 'YES' || headStart === 'YES') {
                     console.log("You got away--barely! You live to stroll through the forest another day.");
                 } 
                 else {
                     console.log("You're not fast and you didn't get a head start? "+
                                 "You never had a chance! The troll eats you.");
                 }
                 break;
             default:
                  playAgain = true;
                  console.log("I didn't understand your choice. "+
                              "Hit Run and try again, this time picking FIGHT, PAY, or RUN!");
         //End of switch
         }    
      //End of =do= code-block
      } while( playAgain === true );
// End of =theGame= FUNCTION-BODY
};
//now call/executed theGame
theGame();

Reference

google search
== the Book ==
javascript [your question] site:developer.mozilla.org
the while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

the do-while-loop:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while

== discussions / opinions ==
javascript [your question] site:stackoverflow.com

javascript while vs do while site:stackoverflow.com
http://stackoverflow.com/questions/5599027/the-do-while-statement

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide


#3

it seems logical now. but I pasted whole code into codecadem ant it doesnt work :frowning: