0

When I'm running this I'm getting the following error:

Expected an assignment or function call and instead saw an expression.

var dealer1 = 8;
var dealer2 = 4;
var dealer3 = 6;
var total2 = dealer1+dealer2+dealer3;

if (total2 < 17) {
    ANSWER = 'safe';
}
else if (total2 = 21) {
    ANSWER = 'black jack';
}
else (total2 >= 17 && total2 < 21); {
    ANSWER = 'stop';
}

It's coming from this: else (total2 >= 17 && total2 < 21) and I can't figure out whats wrong with this! Is this not correct?

1
  • 2
    Else has no condition. It simply covers all other possibilities that your if and else if don't. Commented Mar 19, 2015 at 11:26

3 Answers 3

1

The problem is your last else statement, there should be no conditional expression:

else (total2 >= 17 && total2 < 21); {
    ANSWER = 'stop';
}

should be either written as:

else if(total2 >= 17 && total2 < 21){
    ANSWER = 'stop';
}

or:

else{
    ANSWER = 'stop';
}

depending on what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

You've simply omitted the if after the else.

You also have an extraneous semi-colon after the (expression)

Oh, and there's no branch for a "bust" hand when total2 > 21.

Comments

0

I can see a few problems here...

The first 'else if' block of code is not checking anything it's setting the variable total2 to 21, so if the code gets to this point, ANSWER will always be 'black jack' - to check a value you need to use == or === (strict comparison)

Instead of...

else if (total2 = 21) {
    ANSWER = 'black jack';
}

Use...

else if (total2 == 21) {
    ANSWER = 'black jack';
}

The other problem is here:

else (total2 >= 17 && total2 < 21); {
    ANSWER = 'stop';
}

As others have stated - else does not use a semi-colon so remove that. Whats more, a single else does not have any parameters - it's the final case when all if's and else if's have failed. So you have two options...

Remove the parameters and just have an else { ... } like so:

else {
    ANSWER = 'stop';
}

Or, if you DO need to check for another condition, change it to another else if( ... ) { ... } like so (remembering to remove that semi-colon):

else if (total2 >= 17 && total2 < 21) {
    ANSWER = 'stop';
}

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.