(Solved) Code your own Adventure 2, 6/6 - Just want some extra knowledge on loops


#1

Hi everyone, I tried to somewhat recreate a pokemon battle scene (well, sort of), here's the code:

`var user = prompt("Le wild zigzagoon appears, what shall the trainer do?! RUN, FIGHT or CATCH that pokemon?!").toUpperCase();

var battle = true;
var damage = Math.floor(Math.random() * 30 + 1);
var totalDamageThisRound = 0;

var enemyDamage = Math.floor(Math.random() * 1000 + 1);
var totalEnemyDamageThisRound = 0;

switch(user) {
case 'RUN':
var userEscape = prompt("Are you sure you want to do this? Just answer YES or NO?").toUpperCase();
var escapeRoute = prompt("Shall the trainer call in the chopper? YES or NO?").toUpperCase();

    if (userEscape && escapeRoute === "YES") {
        console.log("The chopper has arrived but zigzagoon used whirlwind to send the chopper flying away! The trainer can't escape now!");
    } else if (escapeRoute === "NO") {
        console.log("The trainer is unable to escape without air support!");
    } else if (userEscape === "YES" || escapeRoute === "YES") {
        console.log("The trainer can't possbily escape the clutches of zigzagoon");
    } else {
        console.log("Then be a man and continue fighting!");
    }

    break;
case'FIGHT':
    var pokemon = prompt("Choose your pokemon! PIKACHU, MAGIKARP or POOCHEYENA?").toUpperCase();

    while(battle) { 
        if (pokemon === "PIKACHU") {
            var attack = prompt("Go Pikachu! What shall it do? Use THUNDERBOLT, THUNDERSHOCK or SCRATCH?").toUpperCase();
            switch(attack) {

                case 'THUNDERBOLT':
                    console.log("Pikachu use THUNDERBOLT! Pikachu have dealt " + damage + " damage to zigzagoon!");
                    totalDamageThisRound += damage;
                    break;

                case 'THUNDERSHOCK':
                    console.log("Pikachu use THUNDERSHOCK! However Zigzagoon is not effected by paralysis!");
                    break;

                case 'SCRATCH':
                    console.log("PIkachu use SCRATCH! It's not very effective...");                      break;

                default: 
                    console.log("I'm afraid pikachu does not understand that command!");
            }
        } else if (pokemon === "MAGIKARP") {
            var attack = prompt("Go Magikarp! What shall it do? Use SPLASH or TACKLE?").toUpperCase();
            switch(attack) {

                case 'SPLASH':
                    console.log("Magikarp use splash! It's not very effective...");
                    break;

                case 'TACKLE': 
                    console.log("Magikarp use tackle! Magikarp have dealt " + damage + " to zigzagoon!");
                    totalDamageThisRound += damage;
                    break;

                default:
                    console.log("Magikarp is confuse about that command!");
            }
        } else if (pokemon === "POOCHYENA") {
            console.log("Poochyena refuses to obey the trainer and remains in the pokeball!");
        } else { 
            var pokemon = prompt("You shouldn't have done that...");
        }

        if (totalDamageThisRound >= 25) {
            console.log("Super effective! Zigzagoon has fainted!");
        } else {
            console.log("Zigzagoon use HYPERBEAM, and dealt " + enemyDamage + " damage to " + pokemon + "!");
            totalEnemyDamageThisRound += enemyDamage;

            if (totalEnemyDamageThisRound >= 500) {
                console.log("The trainer along with the pokemon has fainted");
            } else {
                console.log("The trainer throws a masterball and successfully captures Zigzagoon!");
            }
        }
        battle = false;
    }
    break;
case'CATCH':
    console.log("Zigzagoon deflects the pokeball away");
    break;
default:
    console.log("The trainer is not able to comprehend what is that response, please choose either three.");

}`

Now I just want to highlight on this part:

if (totalEnemyDamageThisRound >= 500) {
console.log("The trainer along with the pokemon has fainted");
} else {
console.log("The trainer throws a masterball and successfully captures Zigzagoon!");
}

Provided if totalEnemyDamageThisRound is less than 500, what can be done to run the loop again, from the beginning, where the user is asked the same question and given the same choice as before? Kind of like the player and the enemy taking turns to make their move until either one is defeated.


#2

while (fight is not over) {
    play another round
}
assess results of fights, who won etc

#3

I'm sorry, I'm not quite clear with your example. Would you mind giving a more clear-cut example?


#4

You seemed to be asking for how to make a loop that runs until the fight is over.

It'd look like that. Just replace the English text with the corresponding conditions/actions in code.

You stated your condition as:

So that's what goes into the condition for the while loop


#5

Oh, I understand. Actually, I just found another way of solving my problem by just typing in the continue statement, but thanks for your help anyways! :smiley: