The Deck of Cards
Well, I don’t know what to say here.
I’ve been staring at the screen for over an hour now. Nothing comes to me.
Back in Module 1 there was an optional exercise in which that involved building a virtual deck of cards and writing some basic methods with it. I was stuck on it for a good while before having to move on to the next lab. I guess that’s what I’m going to write about today.
Building the deck was easy enough. I quickly sketched out how to do it in hashes:
I took the same code and turned it into a deck of objects here:
The problem occurred when I tried to pass the last test in the lab. Creating a method that removes one card from the deck.
It seems simple. Everything else is passing. The deck initializes with an array of 52 card objects. An earlier test checks this condition and it comes out true, it passes. It should just be a matter of using .pop on that array to remove one object. And yet, when I do exactly that, the card count miraculously goes from 52 down to 50.
I tried shamelessly turning the test green by adding one card after the line that dot pops one off the deck. Initially, this threw an error in which the count of the cards went up to 54. The math around this seemed impossible. Why was the deck culling two cards every time I commanded the code to remove one? And why was the count coming to 54 after adding one card? And was this adding the card after removing one card, as it was intended? If one card counted as two when being removed, that would mean that one card counted as six when being added. None of this made sense.
I decided to scrap the whole thing and rebuild it from scratch and kept getting the error in which removing a card lowers the count by two, but now adding a new card after the removal increases the count of cards up to 52. I’m not sure if this is progress or not, although I wish I had taken a screenshot of the original error that returned 54 cards.
I intended to finish this post with a solution, but nothing comes to me. I have 8 minutes before class begins and the blog has come due. Perhaps I will revisit this perplexing error in a future blog post, when I’m more experienced, and I’ll laugh about whatever rookie mistake I’ve been making.