0

I'm working on trying to write some code so that should be relatively easy to do but its not wanting to play nice.

I currently have a "reset" button that just resets the whole page but that's not what I want as I have a random number generator that is static until the page is reset and if you haven't gone through all the numbers you have to run it again and well...its obviously random so reseting the whole page has an unintended consequence.

My current code is as follows.

onclick="window.location.reload(true)"

that's for the reset function on a button. It works...but like I said it has some unintended consequences.

What I have been trying is rewriting the existing function as a new function that when the button is clicked with id="go" the cards will flip that have the class hover then will run the shuffle function.

I've rewritten the code several times and its still not doing what I want so I'm asking for help. I'm including the link to the codepen with the working model of existing code. It does not include the reset code since its broken...figured if you had a fresh look at the working code you may have more luck than I.

Codepen.io

Kyle Drew
  • 370
  • 2
  • 3
  • 16

1 Answers1

0

Have you tried attaching a function to the "Reset" button that re-generate the random number and then re-shuffle the cards ? Modify the shuffle function to take an array argument of all the cards that need to be shuffled.

<button class="btn btn-danger" onClick="reset()">Reset</button>

function reset() {
  $(".shuffleImg").each(function() { 
    $(this).find(".hover").removeClass('hover');
  });
  window.setTimeout(function(){
    shuffle();
  }, 800);
}

This code above is just a simple sudo code and is not pertained to any programming languages.

jeffong
  • 49
  • 1
  • 5
  • Reason I haven't done that is because I want the cards to flip back over and and then be shuffled. Essentially there is two functions that happen in sequential order once the button is clicked. The random number generator separate from the card game. It just effects it too when I reload the page. – Kyle Drew Jun 29 '19 at 19:14
  • Wouldn't a function that removes all `hover` class from the DOM and then re-shuffle the cards work? There will be two loops that run sequentially. All code below an `each` function will not be executed until the `each` loop is done. – jeffong Jun 29 '19 at 19:22
  • possibly...was looking at [this](https://stackoverflow.com/questions/3910736/how-to-call-multiple-javascript-functions-in-onclick-event) – Kyle Drew Jun 29 '19 at 19:31
  • @KyleDrew I have updated my answer with a example snippet of code that works as what you have described ( tested in Codepen ). If I understand your question correctly. – jeffong Jun 29 '19 at 19:31
  • Huh..I was making it a lot more complicated than I needed to apparently – Kyle Drew Jun 29 '19 at 19:53