0

Fellow Coders! super noob here. I have a hard time comprehending

card['card ' + i] + i

in the following problem. Can anyone help me out why is there a +i in the else statement, please?

Your current client is a casino that needs you to fix their Black-Jack card counting program. The function should take an array of objects, each object is a card that was dealt and its value is the number on the card.

The way card counting works is that each card has a weighted value. Cards 2 through 6 are worth one point; 7, 8 and 9 are worth zero; and 10s, face cards, and Aces are worth negative one. This type of card-counting system is called the HiLo count. When counting down a standard deck of 52 cards, you'll notice the count will go up and down, and always end at zero if you counted correctly.

The cardCounter function is supposed to take an array of card objects and return the count (weighted value) of the cards. It doesn't seem to be producing accurate results, see if you can fix it before the card-sharks beat the house!

function cardCounter(array) {
    var count = 0;
    for (var i = 0; i < array.length; i++) {
        var card = array[i];

        if (card['card ' + i] >= 2 && card['card ' + i] <= 6) {
            count++;
        } else if (card['card ' + i] + i >= 10 || card['card ' + i] + i === 'face or ace') {
            count--;
        }
        
    }
    return count;
}

function assertEquals(actual, expected, testDescription) {
    if (actual !== expected) {
        console.log('Test failed:', testDescription);
        return;
    }
    console.log('test passed');
    return;
}

var dealtCards = [ { 'card 0': 2 }, { 'card 1': 6 }, { 'card 2': 8 }, { 'card 3': 'face or ace' } ];

var actual = cardCounter(dealtCards);
var expected = 1;
assertEquals(actual, expected, 'function should return correct HiLo count.');

var dealtCards2 = [ { 'card 0': 'face or ace' }, { 'card 1': 9 }, { 'card 2': 8 }, { 'card 3': 'face or ace' } ];

var actual2 = cardCounter(dealtCards2);
var expected2 = -2;
assertEquals(actual2, expected2, 'function should return correct HiLo count.');
Barmar
  • 741,623
  • 53
  • 500
  • 612
XYZ
  • 9
  • 1
  • To access `"card 0", "card 1", "card 2", ..., "card N"`. – zero298 Oct 05 '21 at 18:23
  • 2
    That array with an object makes little sense. That is a bad structure for data. – epascarello Oct 05 '21 at 18:47
  • Additionally this should not have been marked as duplicate. – Brice Oct 05 '21 at 19:11
  • @zero298 That is not what is occurring. The additional `+1` found in the `else` statement is impacting the value returned from the array reference, not the reference itself. This is causing an incorrect comparison from taking place. – Brice Oct 05 '21 at 19:20
  • @Barmar this should not have been marked as duplicate. It is a very good question from a first time poster that stumbled onto a bug in their code that was giving them a false positive, and closing it is not correct. – Brice Oct 05 '21 at 19:37

1 Answers1

1

You're actually 100% correct. The + i found at the end of card['card ' + i] + i & card['card ' + i] + i is a mistake. In fact, the reason the tests are passing is because the collection of cards that are being used have an 8 & 9 in them and that extra + i is actually causing the correct count to be found purely by coincidence.

If you add an additional test case that looks like this:

var dealtCards3 = [ { 'card 0': 'face or ace' }, { 'card 1': 9 }, { 'card 3': 'face or ace' } ];
var actual3 = cardCounter(dealtCards2);
var expected3 = -2;
assertEquals(actual3, expected3, 'function should return correct HiLo count.');

It throws an error.

The + i are incorrect and should be removed.

Brice
  • 940
  • 9
  • 22