Closed spenserg92 closed 9 months ago
You have some nice code so far, but can you explain to me what this block is meant to be doing?
if (selectedCards.length === card.dataset.revealed){
messageEl = 'Congrats, You Win!'
console.log(messageEl)
}
I'm trying to check for a winner. So once all the cards are revealed a 'winner' message will appear. I'm not sure what i need to select to compare in order to get a message to appear. The message is supposed to be displayed in an h2 element. That is what the messageEl is set to.
So for now I'd focus less on displaying the message and more on the logic that determines if there is a winner. What I see right now is that on every turn you check for
// if there is a match
if (selectedCards[0].dataset.revealed === selectedCards[1].dataset.revealed)
// if there is not a match (and we assume game play isn't over)
else {
// if the length of the selectedCards array is equal to the image on the card?
if (selectedCards.length === card.dataset.revealed)
As you can imagine, that last condition doesn't make a whole lot of sense for checking if the game is over. Here you might want to check for something like if all of the cards have been flipped, or even better, if you have found all 9 matches (perhaps you need to implement a counter that goes up every time there is a successful match...)?
Also that if
,else
, if
might be better re-written as if
, else if
, and else
...
I'm not sure how the 'else if' would work tbh. But I was able to find a way to check for a win and display a winner message.
What's the problem you're trying to solve?
Trying to get my winner result to display
Post any code you think might be relevant (one fenced block per file)
If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?
no results
What is your best guess as to the source of the problem?
I don't really know
What things have you already tried to solve the problem?
placing the if statement in different spots
Paste a link to your repository here
https://github.com/spenserg92/Memory-Game