Syntax Error!


#1

Hi,
I have no idea what I'm doing wrong. I keep getting an "Unexpected token else" error even when I remove the curly brackets. Please help!


Unexpected token else


var slaying= true
var youhit = Math.floor(Math.random() *2)
var damageThisRound= Math.floor(Math.random()*5 +1)
var totalDamage=0 

while(slaying){
    console.log("You're Slaying")
    slaying=false }
    
    if(youHit) {
        console.log("You hit the dragon!")
         totalDamage += damageThisRound }
        if(totalDamage>=4){
            console.log("You slew the dragon")
            slaying=false }
            {else  { 
            (youHit=Math.floor(Math.random()*2))
            }
            
            {else  {
                console.log("You're fried")
                slaying=false
            }


#2

Syntax for an if/else statement should be something like this:

if (condition){
code to run;
} else {
other code;
}


#3

This while loop is rather pointless as it just prints "You're Slaying" once.

The general idea of this "game" is pretty simple:

Pre-match settings
You have a dragon as opponent who has 4 HP.
And at the beginning of your game you first roll a 5 eyed dice to determine your damage (1-5 dmg) and flip a coin to determine whether you hit the dragon or miss.

And then the game starts:
Face the dragon:
Here you have 2 options: Hit or miss!

Miss
When the coin toss was not in your favor the dragon fries you: Game Over! Cruel but simple.
Hit
On the other hand when the coin toss was in your favor, you hit the dragon and reduce it's HP. Side note: Here you don't reduce HP but instead pile up damage until it reaches a threshold, which is a rather "interesting" design choice, but in the end the idea stays the same.

After the Hit:
Apply damage.
Now you have again 2 options: "Slay or Wound"
Slay
If you were able to slay the dragon with this hit: Congratulations! You win! Game Over!
Wound
If you're not able to win with this hit, well try to set another hit:
- toss another coin to determine whether you hit or not
- Go back to "Face the dragon" and repeat the process

How to implement this?
So this last line of "repeat the process" is more or less the trigger word to use a loop, because loops are awesome when it comes to repetitive tasks. So basically everything after "And the game starts" is part of the while loop.

var slaying= true
while(slaying){
...
}

Now you have 2 main options to deal with, as said, "Hit or Miss". Which could be done by an if/else statement. The else is fairly simply: negative Game Over message + ending the loop.
The if again splits into 2 options: "Slay or Wound". So again if/else statement this time nested in the if branch. Where the if branch of the nested one is again: positive Game Over message + ending the loop. And the else part is simply update the settings: toss the coin again.


#4

Thank you both for your help. I actually re-worked the code and got it to pass. :slight_smile:


#5

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