ericek111

Google Forms automatic fill based on probability

Jan 7th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var probs = [
  2.     [0.5, 0.5],
  3.     [0.7, 0.2, 0.1],
  4.     [0.2, 0.5, 0.1, 0.2],
  5.     [[0, 0.3], [0.1, 0.6], 0, [0.5, 1]]
  6. ];
  7.  
  8. (function(e, s) {
  9.     e.src = s;
  10.     e.onload = function() {
  11.         let tosend = {};
  12.         FB_PUBLIC_LOAD_DATA_[1][1].forEach(function(q, qidx) {
  13.             let id = q[4][0][0];
  14.             let ans = "";
  15.             let s = 0.0;
  16.             let r = Math.random();
  17.             console.log(">>>>>>>>>>>> " + Number(r).toFixed(2) + " for " + id + ": " + q[1]);
  18.             if (q[3] == 2) {
  19.                 let aidx = -1;
  20.                 r *= probs[qidx].reduce((total, val) => total + val, 0);
  21.                 q[4][0][1].some(function(choice, ai) {
  22.                     let prob = probs[qidx][ai];
  23.                     aidx++;
  24.                     if (r >= s && r < (s + prob))
  25.                         return true;
  26.                     s += prob;
  27.                 });
  28.                 let answer = q[4][0][1][aidx][0];
  29.                 tosend["entry." + id] = answer;
  30.                 console.log(aidx + " / " + answer + " with " + probs[qidx][aidx]);
  31.             } else if (q[3] == 4) {
  32.                 tosend["entry." + id] = [];
  33.                 q[4][0][1].forEach(function(choice, ai) {
  34.                     let prob = probs[qidx][ai];
  35.                     if (prob === 0)
  36.                         return;
  37.                     if (r >= prob[0] && r <= prob[1])
  38.                         tosend["entry." + id].push(q[4][0][1][ai][0]);
  39.                 });
  40.                 tosend["entry." + id].forEach(function(a, ai) {
  41.                     let prob = probs[qidx][ai];
  42.                     console.log(ai + " / " + a + " with " + prob[0] + " - " + prob[1]);
  43.                 })
  44.             } else {
  45.                 console.log("Unsupported question type " + q[3] + " for " + q[1]);
  46.             }
  47.         });
  48.         jQuery.ajax({
  49.             type: "POST",
  50.             url: FB_PUBLIC_LOAD_DATA_[2] + "/d/" + FB_PUBLIC_LOAD_DATA_[14] + "/formResponse",
  51.             data: tosend,
  52.             traditional: true,
  53.             cache: false
  54.         });
  55.     };
  56.     document.head.appendChild(e);
  57. })(document.createElement('script'), '//code.jquery.com/jquery-latest.min.js');
Add Comment
Please, Sign In to add comment