简体   繁体   中英

Can't figure out whats wrong with Counting Cards Challenge on FreeCodeCamp

I'm currently having an issue with the counting cards challenge on FreeCodeCamp

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards

I created my solution using if else statements and passed 6/8 tests, these were the two that I didn't pass:

Cards Sequence 2, J, 9, 2, 7 should return the string 1 Bet

Cards Sequence 2, 2, 10 should return the string 1 Bet

Can someone explain to me why my code didn't pass these tests, I've looked at it for quite some time and I still don't get why it passed the others but not these two tests.

Thanks!

let count = 0;

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

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

Use === instead of == and use strings instead of numbers. For an explanation, search for the difference between === and ==.

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

and this is the result

// running tests
Your function should return a value for count and the text (Bet or Hold) with one space character between them.
Cards Sequence 2, 3, 4, 5, 6 should return the string 5 Bet
Cards Sequence 7, 8, 9 should return the string 0 Hold
Cards Sequence 10, J, Q, K, A should return the string -5 Hold
Cards Sequence 3, 7, Q, 8, A should return the string -1 Hold
Cards Sequence 2, J, 9, 2, 7 should return the string 1 Bet
Cards Sequence 2, 2, 10 should return the string 1 Bet
Cards Sequence 3, 2, A, 10, K should return the string -1 Hold
// tests completed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM