[Solved] If-statement returning wrong answer [closed]


Your syntax doesn’t allow for that result.

If the first if loop evaluates to true, then the second must be false, and vice-versa.

And since the code within the if loops sets one value to safe and the other to busted, this will always be the case.

I assume that it’s a blackjack style game and would suggest that you evaluate the variables separately, then check to see who the winner is after that.

// Declare static max value the indicates if safe or busted
var MAX = 21;
var c4 =5;
var c5 =1;
var c6 =4;
var d4 =1;
var d5 =11;
var d6 =1;

var total3 = c4+c5+c6;
var total4 = d4+d5+d6;
// Declares 3 variables to hold results for player, dealer and winner
var player="";
var dealer="";
var winner="";

var printResult = function(player, dealer, winner){
    var game1 = "Player: "+ player +", Dealer: "+ dealer + ", " + winner + " has won.";
    return game1;
}

if (total3 > MAX)
{
    player="busted";
}
else
{
    player="safe";
}

if (total4 > MAX)
{
    dealer="busted";
}
else
{
    dealer="safe";
}

if (dealer == 'busted' || (total3 > total4 && player == 'safe'))
{
    winner="player";
}
else
{
    winner="dealer";
}

printResult(player, dealer, winner);

solved If-statement returning wrong answer [closed]