# Blackjack program in javascript(or any language)

**URL:** <https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487>\
**Category:** Uncategorized\
**Created:** [June 20, 2023, 5:04pm UTC](https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487 "2023-06-20T17:04:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 20, 2023, 5:04pm UTC](https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487/1 "2023-06-20T17:04:23Z")

</div>

![image](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/b/7/b7907b5e0cc63fc2e2d6d2410b679d5a0286f58e.png)

```auto
let card_value = 0;
let number_of_cards = Number(prompt("Enter the number of cards."));
if (number_of_cards >= 2 && number_of_cards <= 5) {

  let cards = [];
  for (let i = 0; i < number_of_cards; i++) {
    cards[i] = prompt("Enter the value of card ");
  }
  var dict = {
    "a": (card_value + 11 > 21) ? 1 : 11,
    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5,
    "6": 6,
    "7": 7,
    "8": 8,
    "9": 9,
    "t": 10,
    "j": 11,
    "q": 12,
    "k": 13
  };

  for (let j of cards) {
    if (j == "a") {
      continue;
    }
    else {
      card_value += dict[j];
    }
  }
  // Add the value of 'a' to card_value
  card_value += dict["a"];
  console.log(card_value);
}
else {
  console.log("Program ends here. Please enter again.");
}

```

Here’s what I’ve tried. But it doesn’t works on input a,8,7 which yields 26 wheras it should’ve yield 16.  
What am I missing here? Only logic, no code please.

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 20, 2023, 5:17pm UTC](https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487/2 "2023-06-20T17:17:41Z")

</div>

```auto
let card_value = 0;
let count_of_a = 0;
let number_of_cards = Number(prompt("Enter the number of cards."));
if (number_of_cards >= 2 && number_of_cards <= 5) {

  let cards = [];
  for (let i = 0; i < number_of_cards; i++) {
    cards[i] = prompt("Enter the value of card ");
  }
  var dict = {
    "a": (card_value + 11 > 21) ? 11 : 1,
    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5,
    "6": 6,
    "7": 7,
    "8": 8,
    "9": 9,
    "t": 10,
    "j": 11,
    "q": 12,
    "k": 13
  };

  for (let j of cards) {
    if (j == "a") {
      continue;
      count_of_a++;
    }
    else {
      card_value += dict[j];
    }
  }
  for (let k = 0; k <= count_of_a; k++) {
    card_value += dict["a"];
  }
  console.log(card_value);
}
else {
  console.log("Program ends here. Please enter again.");
}

```

This code works, but I’m confused at the ternary operator part. Why does it work?

---

<div class="post-metadata">

**Author:** ![PAEz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/paez/32/19607_2.png) [@PAEz](https://forum.kirupa.com/u/PAEz)\
**Post date:** [June 20, 2023, 6:04pm UTC](https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487/3 "2023-06-20T18:04:45Z")

</div>

> **[Conditional (ternary) operator - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator)**
>
> The conditional (ternary) operator is the only JavaScript operator that takes three operands:
> a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to...

And your code doesnt work in a few ways, I wont explain because you didnt ask and I think your trying to learn. EDIT: Meh, hints in the edit 😜

EDIT: OH! and understand that any expressions you put in a properties value for an object is only executed when the object is created. So your value for dict.a is always going to be 1 in the current code. ANYTHING after a continue in a for loop does not execute. \<= is different to \<.

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [June 21, 2023, 1:37am UTC](https://forum.kirupa.com/t/blackjack-program-in-javascript-or-any-language/659487/4 "2023-06-21T01:37:30Z")

</div>

```auto
let card_value = 0;
let count_of_a = 0;
let number_of_cards = Number(prompt("Enter the number of cards."));
if (number_of_cards >= 2 && number_of_cards <= 5) {

  let cards = [];
  for (let i = 0; i < number_of_cards; i++) {
    cards[i] = prompt("Enter the value of card ");
  }
  var dict = {

    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5,
    "6": 6,
    "7": 7,
    "8": 8,
    "9": 9,
    "t": 10,
    "j": 11,
    "q": 12,
    "k": 13
  };

  for (let j of cards) {
    if (j == "a") {
      count_of_a++;
      continue;
    }
    else {
      card_value += dict[j];
    }
  }
  for (let k = 0; k < count_of_a; k++) {
    dict["a"] = (card_value + 11 > 21) ? 1 : 11,
      card_value += dict["a"];
  }
  console.log(card_value);
}
else {
  console.log("Program ends here. Please enter again.");
}

```

This one is correct right? I’ve fixed the issues you mentioned above.
