Javascript - counting cards help

Javascript - counting cards help
0

Hi guys!

I’ve been expanding on the Counting Cards lesson from the Javascript section. You can see what I have so far here.

I’ve run into a few problems:

  • Sometimes FULL_DECK[randomCard] returns undefined. I think the problem is with randomCard, but I’m not sure. How do I keep this from happening?

  • When I look at the array, there are a normal number of cards, but sometimes document.getElementById("newCard").innerHTML insists that I pulled 8 Kings in a deck. How do I find out where I went wrong?

Thank you for your time and help!

The problem is you assign an index in the following line of code to randomCard based on the cardsLeft value before removing an element from FULL_DECK, so if randomCard happens to be the last index of the FULL_DECK before the card was removed, then when you try to reference that same index later in the normal result section, there is no value at that index, so you get undefined.

Oooh. Thank you, that helps a lot!

The easiest way to handle this would be to assign the value of the card spliced out to a variable and then used that variable everywhere you currently have FULL_DECK[randomCard].

Thank you! That actually solved my other problem too. I appreciate that you came back with further answers!