Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda