Quick actions

cmd+k|ctrl+k

Navigation

Languages

blackjack calc

Snippet info

Language

JavaScript

Visibility

public

Author

scott.ruiyi.zheng

Created

2017-06-14T05:05:19Z

Updated

2017-06-14T05:05:32Z

function mapping (s){
  if(s === "1"){
    return 1;
  }
  else if(s === "2"){
    return 2;
  }
  else if(s === "3"){
    return 3;
  }
  else if(s === "3"){
    return 3;
  }
  else if(s === "4"){
    return 4;
  }
  else if(s === "5"){
    return 5;
  }
  else if(s === "6"){
    return 6;
  }
  else if(s === "7"){
    return 7;
  }
  else if(s === "8"){
    return 8;
  }
  else if(s === "9"){
    return 9;
  }
  else if(s === "10"){
    return 10;
  }
  else if(s === "K" || s === "Q" || s === "J"){
    return 10;
  }
  else if(s === "A"){
    return 11;
  }
}
function handValue (hand) {
  var total = 0;
  for(var i = 0; i < hand.length; i++){
    total += mapping(hand[i]);
  }

  for(var j = 0; j < hand.length && total > 21; j++){
    if(hand[j] === "A")
      total -= 10;
  }
  return total;
}

console.log(handValue(["2", "3", "A", "A", "A"]));
INFO