0

all else statements give an unexpected token error

var men = 50;
alert("You are a Spartan warrior about to be fighting in the Trojan War, you get onto             your ship and set sail with a fleet of other ships but your ship breaks away from the others in a horrible storm! You have 50 men on your ship, do not lose more than 40 of them, or else.");
var start = confirm("are you ready to start?");
if (start = true) {
   alert("your platoon of Spartan soldiers is on a ship, headed for Troy");
}
else {
   alert("You failed in your mission and are executed in Sparta the next day (refresh the page)")
};
var hydra  = prompt("you encounter a large hydra on your journey to Troy, do you sail past it, or fight it?(fight or flee?)");
if (hydra = "fight") {
   alert("You kill the Hydra, but it has killed 8 of your men");
};
else {
   alert("You go around the Hydra, but it snatches up 6 of your men and starts to follow your ship!");
};
if (hydra = "fight") {
   men = 42
}
else {
   men = 44; 
};
console.log(men);
Pigueiras
  • 18,778
  • 10
  • 64
  • 87

3 Answers3

4

First thing (not a syntax error, but wrong anyway): Compare using == instead of =.

Your syntax error is that you're using ; after the if blocks – remove those.

thejh
  • 44,854
  • 16
  • 96
  • 107
  • 1
    You should probably replace `=` with `===` to prevent any unexpected type conversions creeping in. See here: http://stackoverflow.com/a/359509/111219 – Rob Harrop Mar 29 '13 at 12:50
2

The unexpected token error is because you end the else's with }; but should only be }

Nelson
  • 49,283
  • 8
  • 68
  • 81
0

replace if (start = true) with if (start == true) instead. And }; but should only be }

Mostafa Shahverdy
  • 2,687
  • 2
  • 30
  • 51
PSR
  • 39,804
  • 41
  • 111
  • 151