mardi 24 mars 2015

Javascript beginner while loops


I am new to programming and i tried to code a little game in JavaScript. I stucked on while loops, I'm not getting any errors but somehow they're not working as I want. I'm adding code below:



window.alert("Troll has 20 health, you have 10");
var user = prompt("How you want to beat troll, sword, bow or spear").toLowerCase();
var fight = true;
var atkdamage = Math.floor(Math.random() * 5);
var totaldamage = 0;
var totaldamagercv = 0;
switch (user) {
case 'sword':
window.alert("cool choice, let's start..");
while (totaldamage < 20) {
window.alert("You hit " + atkdamage);
window.alert("Troll has " + (20 - atkdamage) + "health left");
totaldamage += atkdamage
window.alert("TOTAL DAMAGE" + totaldamage);
}
if (totaldamage >= 20)
{
window.alert("You win.");
}
break;
case 'bow':
window.alert("coward but effective, let's start..");
while (totaldamage < 20) {
window.alert("You hit " + atkdamage);
window.alert("Troll has " + (20 - atkdamage) + "health left");
totaldamage += atkdamage
}
if (totaldamage >= 20)
{
window.alert("You win.");
}
break;
case 'spear':
window.alert("smart choice, let's start..");
while (totaldamage < 20) {
window.alert("You hit " + atkdamage);
window.alert("Troll has " + (20 - atkdamage) + "health left");
totaldamage += atkdamage
}
if (totaldamage >= 20)
{
window.alert("You win.");
}
break;
default:
window.alert("what?");
}


Could you please answer which part of the code is wrong?


Thank you very much.





Aucun commentaire:

Enregistrer un commentaire