Blackjack

by Kiya_Iekret

HTML

Hand #1: <input type='number' id='hand1' min='6' max='21'/><br/>
Hand #2: <input type='number' id='hand2' min='6' max='21'/><br/>
Dealer: <input type='number' id='dealer' min='6' max='21'/><br/>
<button>Inflict math</button>
<button>Clear inputs</button>

<p>
  best call: <span></span>
</p>

JavaScript

const hand1_input = document.getElementById('hand1');
const hand2_input = document.getElementById('hand2');
const dealer_input = document.getElementById('dealer');
const output_slot = document.getElementsByTagName('span')[0];

const draw1 = 1/13;
const odds_of_draw = [draw1, draw1, draw1, draw1, draw1, draw1, draw1, draw1, draw1, draw1 * 4];
function bustOddsChecker(hand)
{
// 1 = guaranteed no bust
	const val_to_bust = ((20 - hand) < 10)? 20 - hand : 10; //21 - 1 to account for arr[0]
  let temp_odds_arr = [];
  let temp_odds_holder = 0;
	for (i = 0; i < val_to_bust; i++) {temp_odds_arr.push(odds_of_draw[i]);};
  if (temp_odds_arr.lenght > 0)
  {
  	temp_odds_holder = temp_odds_arr.reduce((accumulator, curVal) => {return accumulator + curVal;});
  }
  else {temp_odds_holder = 0};
	return temp_odds_holder;
};

const calc_trigger = document.getElementsByTagName('button')[0].addEventListener('click', () =>
{
	const hand1 = hand1_input.value * 1;
  const hand2 = hand2_input.value * 1;
  const dealer = dealer_input.value * 1;
  if ((hand1 >= dealer)||(hand1 >= dealer))
  {
  	let hand1_call = (bustOddsChecker(hand1) >= bustOddsChecker(dealer)) ? true : false; //true = hit
    let hand2_call = (bustOddsChecker(hand2) >= bustOddsChecker(dealer)) ? true : false; //true = hit
    if (hand1_call)
    {
    	if (hand2_call) {output_slot.innerText = 1} else {output_slot.innerText = 2};
    }
    else if (hand2_call)
    {output_slot.innerText = 3}
    else {output_slot.innerText = 4};
  }
  else
  {
  	//hit both hands, but not that of the dealer
  };
  //shotCaller(hand1_call, hand2_call);
});

const input_clear = document.getElementsByTagName('button')[1].addEventListener('click', () =>
{
	const inputs = [...document.getElementsByTagName('input')];
  inputs.forEach((dom_ele) =>
	{
		dom_ele.value = "";
	});
  output_slot.innerText = "";
});