Sort of stuck here. Having trouble implementing a while loop into my game


#1


alert("You are playing 'The Last Hero'");
function hero(health, speed, armor) {
    this.health = health;
    this.speed = speed;
    this.armor = armor;
}
var barbarian = new hero(50, 12, 3);
var valkyrie = new hero(30, 20, 1);
var knight = new hero(42, 15, 2);

var player = prompt("You can choose from three classes, KNIGHT, VALKYRIE and BARBARIAN. Which do you choose?").toUpperCase();

if (player === "BARBARIAN") {
    confirm("Here are the stats of your choice.");
    confirm("Health: " + barbarian.health + "," + "Speed: " + barbarian.speed + "," + "Armor: " + barbarian.armor + ".");
} else if (player === "VALKYRIE") {
    confirm("Here are the stats of your choice.");
    confirm("Health: " + valkyrie.health + "," + "Speed: " + valkyrie.speed + "," + "Armor: " + valkyrie.armor + ".");
} else if (player === "KNIGHT") {
    confirm("Here are the stats of your choice.");
    confirm("Health: " + knight.health + "," + "Speed: " + knight.speed + "," + "Armor: " + knight.armor + ".");
} else {
    confirm("?")
}
 

function deadly(damage, weight, ammo, fireRate) {
    this.damage = damage;
    this.weight = weight;
    this.ammo = ammo;
    this.fireRate = fireRate;
}
var fireSword = new deadly(75, 20, 0, 1);
var lightningBow = new deadly(50, 15, 100, 0.5);
var antimatterRifle = new deadly(200, 30, 25, 5);
var weapon = prompt("Do you choose a FIRE SWORD, LIGHTNING BOW or ANTIMATTER RIFLE? Please answer as shown.").toUpperCase();
if (weapon === "FIRE SWORD") {
    confirm("Here are the stats of your choice.");
    confirm("Damage: " + fireSword.damage + "," + "Weight: " + fireSword.weight + "," + "Ammo: " + fireSword.ammo + "," + "Firerate: " + fireSword.fireRate + ".");
} else if (weapon === "LIGHTNING BOW") {
    confirm("Here are the stats of your choice.");
    confirm("Damage: " + lightningBow.damage + "," + "Weight: " + lightningBow.weight + "," + "Ammo: " + lightningBow.ammo + "," + "Firerate: " + lightningBow.fireRate + ".");
} else if (weapon === "ANTIMATTER RIFLE") {
    confirm("Here are the stats of your choice.");
    confirm("Damage: " + antimatterRifle.damage + "," + "Weight: " + antimatterRifle.weight + "," + "Ammo: " + antimatterRifle.ammo + "," + "Firerate: " + antimatterRifle.fireRate + ".");
} else {
    confirm("Sry, Whaa????")
}
function enemy(health, damage, armor, speed) {
    this.health = health;
    this.damage = damage;
    this.armor = armor;
    this.speed = speed;
}
var goblin = new enemy(50, 20, 1, 5);
var fight = prompt("It's time you did your fight. You're first enemy is a goblin(they're weak, don't worry!), do you attack, or block?").toUpperCase();
if (fight === "ATTACK") {
    if (weapon === 'potato' )
    confirm("You did " + weapon.damage + " damage!");
    if (weapon.damage >= goblin.health) {
        confirm("You defeated the goblin, congrats!");
    } else {
        confirm("He's still alive, try again!");
    }
}
// I know what is a while loop, but I don't know how to make a while loop that repeats itself if goblin.health is greater than thing.damage. I want the loop to save the amount of damage dealt to the goblin and repeat itself until the goblin is dead. How do I do this?

// would appreciate any advice that you can think of.
.


#2

Let's say I want to create a while loop that loops when variable is greater than 10. We would assume that variable has more than one thing assigned to it.

It should be simple but the while loop would look like
while (condition(variable > otherVar)) {
console.log(what ever you want to repeat)
Also inside of the while loop you need to make sure that the loop will be ended so I would either have to know that the variables change so that it will be false eventually, or I have to declare that the condition can be proven false
}

I do have a question about this exercise.... Is it required you use a while loop? I feel like you might be able to get the job done with a for loop, but I might be wrong.

I hope this helps. :slight_smile:


#3

you could implant a do while loop:

do {
   // prompt user
} while( // condition )

at this point in the course, this should be covered in the course, not? The advantage of the do while loop is that you prompt the user at least once, and then for as long as needed.

What exactly is it you are struggling with? You can always revisit the exercise of the loops


#5

it is. Since you need user input, and then want to validate it. The do while loop does exactly this.

the tricky part is to write the good condition. That can a bit tricky, @bytebot let us know if you need further help with the condition of your do while loop

You are not going to read the personal message i send you?


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.