Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

366
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda