Syntax Error - Unexpected Token Else


#1

Here is the code I used to create my game. Can anyone help me out here?

var baseAttack = 0;

var baseSpeed = 0;

var baseIntell = 0;

var attackDamage = math.random;

var dealtDamage = attackDamage * baseAttack;

var monsterHealth = 20;

var user = prompt("'What is your name, fellow adventurer?'");

var intell = prompt("'How smart do you believe to be, " + user + "? Very? About so? or not so smart?'").toUpperCase();

switch(intell) {
case "VERY":
console.log("'You are a smart person then. Congrats!'")
baseIntell = 3;
break;
case "ABOUT SO":
console.log ("'Mhm. Not smart, not dumb. A good middle, hmm?'");
baseIntell = 2;
break;
case "NOT SO SMART":
console.log ("'Oh, sorry to hear that.'");
baseIntell = 1;
break;
default:
console.log ("Invalid response. Please respond with (NOT SO SMART), (VERY), or (ABOUT SO).");
break;
}
var strength = prompt("So, tell me, how strong are you? (Use the same indicators as before.)").toUpperCase();

switch (strength) {
case "NOT SO STRONG":
baseAttack = 1;
console.log("I see.");
break;
case "VERY":
baseAttack = 3;
console.log("Oh, I see!");
break;
case "ABOUT SO":
baseAttack = 2;
console.log("So-so, it seems?");
break;
default:
console.log("Ah! You didn't pick a proper indicator! Try again!");
break;
}

var speed = prompt("So, how fast are you?").toUpperCase();

switch(speed) {
case "VERY":
baseSpeed = 3;
console.log("Hmm.. Being fast sure is nice, huh?");
break;
case "ABOUT SO":
baseSpeed = 2;
console.log("Mhm. Makes sense, y' know.");
break;
case "NOT VERY FAST":
baseSpeed = 1;
console.log("Slow... Seems kinda a hinderance, right?");
break;
default:
console.log("You didn't select a proper indicator! Try again!");
break;
};

var battleOption = prompt("A troll gets in the way! What do you do? (Attack, Defend, Item, Run)").toUpperCase();

switch(battleOption) {
case "ATTACK":
if (baseAttack = 3) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};
if else(baseAttack = 2) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};
if else(baseAttack = 1) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};
else {
console.log("You were defeated, since you weren't strong enough..");
};
break;
case "RUN":
if(baseAttack && baseSpeed = 2) {
console.log("You were able to escape, due to your skills!");
}
if else(baseSpeed || baseIntell = 2) {
console.log("You escaped, due to being so fast and smart!");
}
else {
console.log ("You were unable to escape!");
}
break;
default:
console.log("You didn't select a proper indicator! Try again!");
break;
}

And here is the syntax error I received:

SyntaxError: Unexpected token else

I checked over the code to see if I messed anything up, for at least an hour. But I couldn't find out what was wrong!


#2

Pretty long code so I read it quickly but here is what I can say :

1) Many of your switches based on character characteristics are useless since you calculate the damages in 'var dealtDamage'.

if (baseAttack = 3) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};
if else(baseAttack = 2) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};
if else(baseAttack = 1) {
console.log("You did " + dealtDamage + " damage!");
monsterHealth = monsterHealth - dealtDamage;
console.log("The monster has " + monsterHealth + " health left!");
};

Look, it's exactly the same code for the 3 cases.

2) Double check the if() in your switches too :

if (baseAttack = 3) {...}

You must use '==' to check the value of baseAttack. '=' is to attribute a value.

Does it solve your problem?


#3

Ah, didn't realize I made that first mistake. Made this early in the morning and was pretty flustered. /:
But, thank you!