Basic JavaScript - Counting Cards

Tell us what’s happening:
When I tried running this code in Google chrome then this code passed all the TEST given on FreeCodeCamp website but when I am submitting this code on https://www.freecodecamp.org/ then it is giving following output in console and I am not able to submit this code.

 **Output in Console on  freecodecamp.org**

// running tests
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 3, 2, A, 10, K should return the string -1 Hold
// tests completed

  **Your code so far**


let count = 0;

function cc(card) {
// Only change code below this line

      const arrPlus=[2, 3, 4, 5, 6];
      const arrZero=[7, 8, 9];
      const arrMinus=[	10, 'J', 'Q', 'K', 'A'];
      arrPlus.forEach(funcPlus);
      function funcPlus(value, index, array){if (value==card){count++;}}
      arrMinus.forEach(funcMinus);
      function funcMinus(value, index, array){if (value==card){count--;}}
      arrZero.forEach(funcZero);
      function funcZero(value, index, array){if (value==card){count-=0;}}

      if (count>0){return count+' Bet';} 
      else if(count<=0 ){return count+' Hold ';}


// Only change code above this line
}


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Counting Cards

Link to the challenge:

Oops.

Its a bit inefficient, but this is certainly a unique solution.

you have an extra space at the end of your output


learning to format your code makes understanding you much easier
here your code nicely formatted for example

function cc(card) {
  // Only change code below this line

  const arrPlus = [2, 3, 4, 5, 6];
  const arrZero = [7, 8, 9];
  const arrMinus = [10, 'J', 'Q', 'K', 'A'];
  arrPlus.forEach(funcPlus);

  function funcPlus(value, index, array) {
    if (value == card) {
      count++;
    }
  }
  arrMinus.forEach(funcMinus);

  function funcMinus(value, index, array) {
    if (value == card) {
      count--;
    }
  }
  arrZero.forEach(funcZero);

  function funcZero(value, index, array) {
    if (value == card) {
      count -= 0;
    }
  }

  if (count > 0) {
    return count + ' Bet';
  } else if (count <= 0) {
    return count + ' Hold ';
  }


  // Only change code above this line
}
1 Like

Sir It still don’t solves the issue . I am not able to submit it on freecodecamp whereas this code solves the problem.

Did you fix this problem?

1 Like

yes I did it count+' Hold' now in original code space is not there still it does not pass the test

I don’t know what to tell you. When you correctly fix that bug, the code works:

Thanks @JeremyLT I also tried it now it passed

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.