sei-ec-remote / project-1-issues

Open new issues here
1 stars 2 forks source link

Can't get winner result to display #227

Closed spenserg92 closed 9 months ago

spenserg92 commented 9 months ago

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)

    selectedCards.push(card)
    if (selectedCards.length === 2) {
        if (selectedCards[0].dataset.revealed === selectedCards[1].dataset.revealed) {
            selectedCards = []
        }
        else {
            setTimeout(()=>{
                selectedCards.forEach((c) => {
                    c.style.backgroundImage = "url(" + background + ")"
                })
                selectedCards = []
            }, 2000)
        }
        if (selectedCards.length === card.dataset.revealed){
            messageEl = 'Congrats, You Win!'
            console.log(messageEl)
        }
    }

}

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

nayaba commented 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)
        }
spenserg92 commented 9 months ago

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.

nayaba commented 9 months ago

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...)?

nayaba commented 9 months ago

Also that if,else, if might be better re-written as if, else if, and else...

spenserg92 commented 9 months ago

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.