Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.75 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="js/jquery-3.1.1.min.js"></script>
  5. <script>
  6. $(document).ready(function() {
  7.   var debugCount = 0;
  8.   // PREBUILT FREQ ARRAY, RUNNING THROUGH CODE SHOULD
  9.   // GIVE A BINGE RESULT OF 8,5 OR 8,6 THROUGH LOOPS
  10.   // AND RANDOMIZATION. {"Value":Frequency}
  11.   var freq = {"1":1,"8":3,"5":2,"6":2,"7":1};
  12.  
  13.   function cleanValue(winner) {
  14.     freq[winner] = 0;
  15.     debugCount++;
  16.     // console.log("clean "+winner);
  17.   }
  18.  
  19.   function getMaxFreq() {
  20.     if (debugCount == 0) {
  21.       var returnObj = {"value":8, "frequency":3};
  22.     } else if (debugCount == 1) {
  23.       var returnObj = {"value":5, "frequency":3};
  24.     } else if (debugCount == 2) {
  25.       var returnObj = {"value":6, "frequency":2};
  26.     } else if (debugCount == 3){
  27.       var returnObj = {"value":7, "frequency":2};
  28.     } else {
  29.       var returnObj = {"value":2, "frequency":1};
  30.     }
  31.     return returnObj;
  32.   }
  33.  
  34.   // Create final array
  35.   var bingeResults = [];
  36.   // Max number of results
  37.   var maxResults = 2;
  38.   // Continue looping until array is complete
  39.   while(bingeResults.length < maxResults) {
  40.     // create temp array
  41.     if (tmpAry == null) {
  42.       var tmpAry = [];
  43.       var tmpAryFreq = [];
  44.     }
  45.     // Get the current max value/freq from the freq array
  46.     // DEBUG: TEMPORARY VALUE, SHOULD BE RETURNED MAX-FREQ OBJECT
  47.     var maxTmpObj = getMaxFreq();
  48.     // Continue adding elements into the array until
  49.     // the returned number is less than the current
  50.     // console.log(maxTmpObj.value + "," + maxTmpObj.frequency + " tmpAry lastValue " + tmpAryFreq[tmpAryFreq.length-1]);
  51.     if (tmpAry == "" || tmpAryFreq[tmpAryFreq.length-1] == maxTmpObj.frequency) {
  52.       // console.log("true");
  53.       tmpAry.push(maxTmpObj.value);
  54.       tmpAryFreq.push(maxTmpObj.frequency);
  55.       // Clean value from freq obj
  56.       cleanValue(maxTmpObj.value);
  57.     } else if (tmpAry.length > 1) {
  58.       // If the new value is not in the array, and the array
  59.       // is larger than 1 item, pick a random item from the
  60.       // temp array
  61.       var newValue = tmpAry[Math.floor(Math.random() * tmpAry.length)];
  62.       // console.log("random " + newValue);
  63.       tmpAry = null;
  64.       tmpAryFreq = null;
  65.     } else {
  66.       // If the new value is not already in the array, and
  67.       // the array only holds 1 value, return it
  68.       var newValue = tmpAry[0];
  69.       // console.log("solo " + newValue);
  70.       tmpAry = null;
  71.       tmpAryFreq = null;
  72.     }
  73.     // If a new value is returned, push it to results array
  74.     if (newValue != undefined) {
  75.       bingeResults.push(newValue);
  76.       newValue = null;
  77.     }
  78.     // Otherwise, continue looping
  79.     // DEBUG: FORCE SCRIPT FAIL
  80.   }
  81.   console.log(bingeResults);
  82. });
  83. </script>
  84. </head>
  85. <body>
  86.  
  87. </body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement