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

369
Visualizações
I can't solve the Counting Cards problem on freeCodeCamp

This is the freeCodeCamp Counting Cards activity. https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards

My solution was

let count = 0;

function cc(card) {
  // Only change code below this line
  if (card = 2 || 3 || 4 || 5 || 6) {
    count + 1;
    } else if (card = 7 || 8 || 9) {
    return;
    } else if (card = 10, "J", "Q", "K", "A")
    count - 1;
}
if (count > 0) {
  return count + " " + "Bet";
} else if {
 return count + " " + "Hold";
}
  // Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');

I checked the solution and it seemed similar. Why didn't it work?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

A Few Modifications were necessary for this code to work.

  1. = is assignment operator while == is comparing operator.
  2. else if can be replaced with just else if there is no condition.
  3. the third else if (card = 10, "J", "Q", "K", "A") didnt have closing or opening brackets.
  4. avoid using return at else if (card == 7 || 8 || 9) { and instead use count += 0
  5. You need too add a condition for || Or operator like this card == 7 || card == 8 and not like card == 7 || 8
  6. Use count++ and count-- instead of count + 1 and count - 1 After all these changes the below code works fine.

let count = 0;
function cc(card) {
  // Only change code below this line
  if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6) {
    count++;
    } else if (card == 7 || card == 8 || card == 9) {
    count += 0;
    } else if (card == 10|| card == "J"|| card == "Q"|| card == "K"|| card == "A"){
    count--;
    }
    if (count > 0) {
      return count + " " + "Bet";
    } else {
      return count + " " + "Hold";
    }
}
cc(2); cc(3); cc(7); cc('K'); cc('A');

about 4 years ago · Juan Pablo Isaza Relatório

0

= is used to assign a value and not to make comparison. In javascript we use == or === to make comparison, and also count - 1 will not update count. use count = count - 1 or count++

let count = 0;    
function cc(card) {
      // Only change code below this line
      if ( card >= 2 && card <= 6) {
        count = count + 1;
        } else if (card >= 7 && card <= 9) {
        return;
        } else if (card === 10 || card === "J" ||  card ===  "Q" || card ===  "K" || card ===  "A")
        count = count - 1;
    }
    if (count > 0) {
      return count + " " + "Bet";
    } else if {
     return count + " " + "Hold";
    }
      // Only change code above this line
    }
    
    cc(2); cc(3); cc(7); cc('K'); cc('A');
about 4 years ago · Juan Pablo Isaza Relatório

0

There are two main issues with your code.

Wrong if condition

Your if condition card = 2 || 3 || 4 || 5 || 6 does not work like you think it does. You would need to use car === 2 || card === 3 || car === 4 || card === 5 || car === 6. You cannot use the assignment operator = like this. You should use the strict comparison operator instead. It's even easier to use an array and includes() like this [2, 3, 4, 5, 6].includes(card).

count is not updated

You need to assign the new value to count when you increment/ decrement it. You can use count++ instead of count + 1 which is the equivalent to count = count + 1.

let count = 0;

function cc(card) {
  // Only change code below this line
  if ([2, 3, 4, 5, 6].includes(card)) {
    count++;
  } else if ([10, "J", "Q", "K", "A"].includes(card)) {
    count--;
  }

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

cc(2); cc(3); cc(7); cc('K'); cc('A');

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