My Adventure's syntax is wrong. Please help!


#1


SyntaxError: expected expression, got keyword 'else'
Oops, try again. There was a problem with your syntax.


I started to create a game. This is the bugged section. 

var battle = function(bin) {
    var bmlvl = bin - Math.round(Math.random());
    var mdam = bmlvl * 10;
    var mblo = bmlvl * 5;
    var macc = bmlvl * 2;
    var mcchance = bmlvl * 1.25;
    var mdeath = false;
    var fmhp = bmlvl * 75;
    var mhp = fmhp
    while (death === false) {
        missCheck(acc);
        if (miss === false) {
            critCheck(cchance);
            if (crit === true) {
                var mhp = mhp - ((dam * 2) - mblo);
                console.log("You make a critical hit!");
                if (mhp < 1) {
                    console.log("You killed the monster!")
                    var death = true;
                }
                console.log("The monster's HP is "+mhp+"/"+fmhp+"!");
            } else if (crit === false) {
            var mhp = mhp - (dam - mblo);
            console.log("You hit the monster!");
                if (mhp < 1) {
                   console.log("You killed the monster!")
                   var death = true;
                } else {
                console.log("The monster's HP is "+mhp+"/"+fmhp+"!");
                }
            } else {
                console.log("CRITICAL ERROR");
            }
        } else if (miss === true) {
            console.log("You miss!");
        } else {
            console.log("MISS ERROR");
        }
        missCheck(macc);
        if (miss === false) {
            critCheck(mcchance);
            if (crit === true) {
                var hp = hp - ((mdam * 2) - blo);
                console.log("The monster makes a critical hit!");
                if (hp < 1) {
                    console.log("YOU HAVE DIED!!! Reload the page to start over.");
                    var death = true;
                } else {
                    console.log("Your HP is "+hp+"/"+fhp+"!");
                }
            } else if (crit === false) {
                var hp = hp - (mdam - blo);
                console.log("The monster hits you!");
                if (hp < 1) {
                    console.log("YOU HAVE DIED!!! Reload the page to start over.");
                    var death = true;
                } else {
                    console.log("Your HP is "+hp+"/"+fhp+"!");
                }
            } else {
                console.log("CRITICAL ERROR");
        } else if (miss === true) {
            console.log("The monster misses!");
        } else {
            console.log("MISS ERROR");
        }
    }
    console.log("You win!");
    var hp = fhp;
    }
}


#2

Hi @benji2602,

I'll try to keep this brief as the code is long :slight_smile:
Please see the sample below with comments. This is a quick review minds you so somethings might have been missed, additionally without the entire code and specifically the argument passed in it's hard to be entirely sure but none the less hopefully this will help and should be self explanatory to someone with your coding skills, finally may I advise possibly a different format for writing to try and help spot the errors easier, for example I always favor braces to be vertically inline and each on it's own line, hence it was simpler to spot the errors :slight_smile:

var battle = function(bin) 
{
    var bmlvl = bin - Math.round(Math.random());
    var mdam = bmlvl * 10;
    var mblo = bmlvl * 5;
    var macc = bmlvl * 2;
    var mcchance = bmlvl * 1.25;
    var mdeath = false;
    var fmhp = bmlvl * 75;
    var mhp = fmhp; //for syntactical purposes add semi-colon
    while (death === false) 
    {
        missCheck(acc);
        if (miss === false) 
        {
            critCheck(cchance);
            if (crit === true) 
            {
                /*var*/ mhp = mhp - ((dam * 2) - mblo); //mhp is being declared twice
                console.log("You make a critical hit!");
                if (mhp < 1) 
                {
                    console.log("You killed the monster!"); //for syntactical purposes add semi-colon
                    var death = true;
                }
                console.log("The monster's HP is "+mhp+"/"+fmhp+"!");
            } 
            else if (crit === false) 
            {
                /*var*/ mhp -= (dam - mblo); //can also try the short cut version
                console.log("You hit the monster!");
                if (mhp < 1) 
                {
                   console.log("You killed the monster!"); //for syntactical purposes add semi-colon
                   var death = true; //death is already defined, though it is in a different scope
                } 
                else 
                {
                    console.log("The monster's HP is "+mhp+"/"+fmhp+"!");
                }
            } 
            else 
            {
                console.log("CRITICAL ERROR");
            }
        } 
        else if (miss === true) 
        {
            console.log("You miss!");
        } 
        else 
        {
            console.log("MISS ERROR");
        }
        missCheck(macc);
        if (miss === false) 
        {
            critCheck(mcchance);
            if (crit === true) 
            {
                var hp = hp - ((mdam * 2) - blo);
                console.log("The monster makes a critical hit!");
                if (hp < 1) 
                {
                    console.log("YOU HAVE DIED!!! Reload the page to start over.");
                    var death = true; //re-use of death re-declared
                } 
                else 
                {
                    console.log("Your HP is "+hp+"/"+fhp+"!");
                }
            } 
            else if (crit === false) 
            {
                var hp = hp - (mdam - blo); //same as death
                console.log("The monster hits you!");
                if (hp < 1) 
                {
                    console.log("YOU HAVE DIED!!! Reload the page to start over.");
                    var death = true; //...we've seen this before no more comments... :)
                } 
                else 
                {
                    console.log("Your HP is "+hp+"/"+fhp+"!");
                }
             } 
             else 
             {
                console.log("CRITICAL ERROR");
             } //missing this closing brace
        } 
        else if (miss === true) 
        {
            console.log("The monster misses!");
        } 
        else 
        {
            console.log("MISS ERROR");
        }
    }
    console.log("You win!");
    var hp = fhp; //...again with hp, but also fhp isn't initialized or defined here? :)
    //} omit this closing brace
}

// battle(5);  //my own call just to see what might happen to the output

#3

I started again with objects. It's a lot easier, but now I get 'Your HP is NaN/400!' for console.log("Your HP is "+p.hp+"/"+p.fhp+"!), and p.hp's value isn't changing.

Also:
p.fhp = 400;
p.hp = p.fhp;

var battle = function() {
var m = new Object();
m.lvl = p.lvl - Math.round(Math.random());
if (m.lvl === 0) {m.lvl = 1;} else {}
m.dam = m.lvl * 10;
m.blo = m.lvl * 5;
m.acc = m.lvl * 2;
m.cchance = m.lvl * 1.25;
m.fhp = m.lvl * 100;
m.hp = m.fhp;
console.log("A level "+m.lvl+" monster attacks you!");
var death = false;
while (!death) {
missCheck(p.acc);
if (!miss.check) {
critCheck(p.cchance);
if (crit.check) {
m.hp -= ((p.dam * 2) - m.blo);
console.log("You make a critical hit!");
if (m.hp < 1) {
console.log("You killed the monster!")
death = true;
} else {
console.log("The monster's HP is "+m.hp+"/"+m.fhp+"!");
}
} else if (!crit.check) {
m.hp -= (p.dam - m.blo);
console.log("You hit the monster!");
if (m.hp < 1) {
console.log("You killed the monster!")
death = true;
} else {
console.log("The monster's HP is "+m.hp+"/"+m.fhp+"!");
}
} else {
console.log("CRITICAL ERROR");
}
} else if (miss.check) {
console.log("You miss!");
} else {
console.log("MISS ERROR");
}
missCheck(m.acc);
if (!miss.check) {
critCheck(m.cchance);
if (crit.check) {
p.hp -= ((m.dam * 2) - p.blo);
console.log("The monster makes a critical hit!");
if (p.hp < 1) {
console.log("YOU HAVE DIED!!! Reload the page to start over.");
death = true;
} else {
console.log("Your HP is "+p.hp+"/"+p.fhp+"!");
}
} else if (!crit.check) {
p.hp -= (m.dam - p.blo);
console.log("The monster hits you!");
if (p.hp < 1) {
console.log("YOU HAVE DIED!!! Reload the page to start over.");
var death = true;
} else {
console.log("Your HP is "+p.hp+"/"+p.fhp+"!");
}
} else {
console.log("CRITICAL ERROR");
}
} else if (miss.check) {
console.log("The monster misses!");
} else {
console.log("MISS ERROR");
}
}
exp(m.lvl * 10);
};


#4

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