-4
function wpnChs() {
var p1 = prompt("Do you take a SWORD or a CLUB to battle?").toUpperCase();
var HP = 1;
var EHP = 1;
var dmg = Math.floor(Math.random() * 1);
var dmgT = Math.floor(Math.random() * 1);
var first = Math.floor(Math.random() * 1);
if (p1 === "SWORD") {
HP = 10;
EHP = 9;
dmg = Math.floor(Math.random() * 8);
dmgT = Math.floor(Math.random() * 6);
first = Math.floor(Math.random() * 3);
} else if (p1 === "CLUB") {
HP = 11;
EHP = 9;
dmg = Math.floor(Math.random() * 6);
dmgT = Math.floor(Math.random() * 5);
first = Math.floor(Math.random() * 3);
} else {
wpnChs();
}
}
function dmgD1() {
EHP -= dmg;
}
function dmgD2() {
HP -= dmgT;
}
function fR() {
if (p1 === "SWORD") {
dmg = Math.floor(Math.random() * 8);
dmgT = Math.floor(Math.random() * 6);
first = Math.floor(Math.random() * 3);
} else {
dmg = Math.floor(Math.random() * 6);
dmgT = Math.floor(Math.random() * 5);
first = Math.floor(Math.random() * 3);
}
}
var fight = function() {
if (first === 0 || 2) {
dmgD1();
if (dmg === 0) {
alert("You attacked, but the enemy dodged it!");
fR();
fight();
} else {
alert("You attacked and did " + dmg + " damage. The enemy now has " + EHP
+
" health");
if (EHP <= 0) {
alert("You killed the enemy!")
} else {
fR();
fight();
}
}
} else {
dmgD2();
if (dmgT === 0) {
alert("The enemy attacked, but you dodged it!");
fR();
fight();
} else {
alert("The enemy attacked and did " + dmgT + " damage. You now have " + HP
+
" health.");
if (HP <= 0) {
alert("You died!");
} else {
fR();
fight();
}
}
}
};
wpnChs();
fight();
難道你不認爲這是一個好主意,顯示你得到什麼錯誤? – NewToJS
Java與JavaScript無關 – epascarello
奇怪的錯誤是什麼? – epascarello