0

I currently have this code below, it is supposed to emulate a card counting system, in which different characters increment/decrement the count when passed. The code below runs successfully, but when I attempt console.log(cc(2,3,4,5,6); it returns 1 Bet, when I know the code runs 5 Bet, as those characters should increment the count, yet console.log does not return the accurate count, I assume this is due to scope? But I would like if someone could explain why count isn't accurately returned.

var count = 0;

function cc(card) {
  switch(card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count += 1;
      break;
    case 7:
    case 8:
    case 9:
      count += 0;
      break;
    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      count -= 1;
      break;
  }

  if (count > 0) {
    return count + " Bet";
  } 
  else {
    return count + " Hold";
  }

}


cc(2,3,4,5,6); \\ returns 5 Bet
console.log(cc(2,3,4,5,6)); \\ returns 2 Bet


ah2140
  • 1
  • 2

3 Answers3

0

var count = 0;
const bet = card => {
  switch (card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      count += 1;
      break;
    case 7:
    case 8:
    case 9:
      count += 0;
      break;
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
      count -= 1;
      break;
  }
};
function cc(...moves) {
  moves.forEach(bet);
  if (count > 0) {
    return count + " Bet";
  } else {
    return count + " Hold";
  }
}

console.log(cc(2, 3, 4, 5, 6)); // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)); // returns 10 Bet
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
0

You have two major problems.

First, your count need to start at zero every time you call cc(). Because you have declared count outside the function, its values is preserved between calls to cc(). By declaring it inside the function, it's initialized to zero before it starts counting the card values.

(Unless you want to continue to the previous count, in which case you should keep it declared outside the function)

Second, your function only accepts one argument. You need to make it accept a list of arguments. This can be done simply with the spread operator .... Then you need to loop through each card value and do the count.

function cc(...cards) { // accept a list of card values as arguments.
  let count = 0

  for (const card of cards) { // loop through all card values.
    switch (card) {
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        count += 1
        break
      case 7:
      case 8:
      case 9:
        count += 0
        break
      case 10:
      case 'J':
      case 'Q':
      case 'K':
      case 'A':
        count -= 1
        break
    }
  }

  if (count > 0) {
    return `${count} Bet`
  }

  return `${count} Hold`
}

cc(2, 3, 4, 5, 6) // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)) // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)) // returns 5 Bet
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • I understand having to insert count within the function in order to preserve it's value, but why did you change var to let? – ah2140 Apr 02 '20 at 20:49
  • No reason specific to this question. `let` is preferred in modern javascript. See [this for more discussion](https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var) – Alex Wayne Apr 02 '20 at 20:51
0

Use arguments or splat operator to get all arguments passed and then loop it:

function cc(...cards) {
  var count = 0;
  var card;

  for (var i = 0; i < cards.length; i++) {
    card = cards[i];
    switch (card) {
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
        count += 1;
        break;
      case 7:
      case 8:
      case 9:
        count += 0;
        break;
      case 10:
      case 'J':
      case 'Q':
      case 'K':
      case 'A':
        count -= 1;
        break;
    }
  }

  if (count > 0) {
    return count + " Bet";
  } else {
    return count + " Hold";
  }
}



cc(2, 3, 4, 5, 6); // returns 5 Bet
console.log(cc(2, 3, 4, 5, 6)); // returns 2 Bet
Justinas
  • 41,402
  • 5
  • 66
  • 96