Blackjack program in javascript(or any language)

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.

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?

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 :stuck_out_tongue_winking_eye:

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 <.

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.