Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Randomization of the deck #6

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open

Add Randomization of the deck #6

wants to merge 1 commit into from

Conversation

Stevec4
Copy link

@Stevec4 Stevec4 commented Feb 20, 2022

I know this is an old project, but I found it useful and wanted to share a small snippet of code to randomize the deck.
add this to the flash-cards.min.js and your deck will have a randomized list of questions.

 $('ul').each(function(){
            // get current ul
            var $ul = $(this);
            // get array of list items in current ul
            var $liArr = $ul.children('li');
            // sort array of list items in current ul randomly
            $liArr.sort(function(a,b){
                  // Get a random number between 0 and 10
                  var temp = parseInt( Math.random()* 5 );
                  // Get 1 or 0, whether temp is odd or even
                  var isOddOrEven = temp%2;
                  // Get +1 or -1, whether temp greater or smaller than 5
                  var isPosOrNeg = temp > 5 ? 1 : -1;
                  // Return -1, 0, or +1
                  return( isOddOrEven*isPosOrNeg );
            })
            // append list items to ul
           .appendTo($ul);  
			//console.log( ul );
      });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants