Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
OBJECT ACCESS BRACKET NOTATION

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.');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!