Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKenneth, Jr Barbier
1,221 PointsSyntax Error when I use else in my if else statement
When I run this code it tells me SyntaxError: unexpected token e
. It's the e
in my else statement. What's going on?
function max(vikings, longShips){
if (vikings > longShips){
return vikings;}
} else {
return longShips;
}
max(10, 5);
1 Answer
jb30
44,807 Pointsfunction max(vikings, longShips){
if (vikings > longShips){
return vikings;}
}
In this section of your code, you have two {
and two }
. Try removing one of the }
between return vikings;
and else
.
Kenneth, Jr Barbier
1,221 PointsKenneth, Jr Barbier
1,221 PointsThank you so much, it runs now! I guess when if else statements are inside of functions you don't need curly braces like this: if(){
} else { }
jb30
44,807 Pointsjb30
44,807 PointsFor single lines of code, you don't need
{
and}
around them. If you have multiple lines of code, then you need the{
and}
.Your code was not of the form
and was of the form
This first has two
{
and two}
. The second has two{
and three}
.