简体   繁体   中英

Writing a function using switch case statement beginner javascript

function hardTotals(dealersCard, myTotal){
switch(dealersCard, myTotal){

case 7 || 8 || 9 || 10 || "A", 16 || 15 || 14:
    return "Hit";
    break;
   }
}
var text = hardTotals(9, 14);
console.log(text);\'''

I am trying to build a basic blackjack card counter. I am using switch case statements to tell the program what to do following basic strategy charts. This is a piece of my function hard Totals where I am trying to say if these cards are the dealers Card and these cards are my Total return hit. This is returning undefined. I am not sure why. I am very new to JS so any advice is appreciated. Thanks.

function hardTotals(dealersCard, myTotal){
switch(dealersCard, myTotal){

case 7 || 8 || 9 || 10 || "A", 16 || 15 || 14: //problem
    return "Hit";
    break;
   }
}
var text = hardTotals(9, 14);
console.log(text);

Hey guys, found the solution. For some reason switch statement will accept 2 arguments but will only accept 1 argument possibility for the second argument. So looks like because 14 was the third option for the second argument it returned undefined. This would be the same output using 15. 16 works for all of the param1 arguments though. Thanks all. Would love advice if anyone has it about a more efficient way to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM