Build a Card Counting Assistant - Build a Card Counting Assistant

Tell us what’s happening:

for some reason when i use more than 2 console log’s on the function it return undefined. This seems to only be happening after the actually +1 an -1 of the count vairable

Your code so far

let count = 0;

function cardCounter (card) {
  if (2 <= card && card <= 6){
      count++;
  }
  else if(10 == card || card == "J" || card == "Q" || card == "K" || card == "A"){
     count--;
  };
// AFTER THIS POINT IT FAILES
  if (count <= 1){
     return `${count} Bet`
  }
  else if (count <= 0){
    return `${count} Hold`
  }
}

console.log(cardCounter(1))
console.log(cardCounter(2))
console.log(cardCounter(5))

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build a Card Counting Assistant - Build a Card Counting Assistant

I did find the problem

  if (count <= 1){
     return `${count} Bet`
  }

My “crocodile teeth” was pointing the wrong way. But I dont under stand why it would trow undefined due to this?

The cardCounter function should return a string with current count and the string Bet if the count is positive.

Please review this instruction.

It didn’t go into the if or the else, so there was no return statement. Functions return undefined when there’s no return statement.

1 Like

Thank you. Is it a normal practice to put a fallback return at the end of a function?
Example.

 return "didnt return anything"

If the function is expected to return a value, it’s common to assign a default value to return, even if it’s an empty array or string, etc. But not all functions are expected to return a value i.e. if they are just performing a task

your two return are here, what would happen if count is higher than 1? what would be returned? that’s where undefined comes from

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