daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest May 17th, 2018 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const myAssignment = document.getElementById('myAssignment');
  2. const myInput = document.getElementById('myInput');
  3. const feedback = document.getElementById('feedback');
  4.  
  5. let assignments = [];
  6. let awnsers = [];
  7. let counter = 0;
  8. let num = 0;
  9. let mySum;
  10. let correctAwnsers = [];
  11. let index = 0; // global assignments counter
  12.  
  13. function init() {
  14.   makeSum();
  15.   console.log(assignments);
  16. }
  17.  
  18. function makeSum() {
  19.   let a = Math.floor(Math.random() * 9 + 2);
  20.   let b = Math.floor(Math.random() * 9 + 2);
  21.   mySum = a + " * " + b;
  22.   myInput.focus();
  23.   assignments[index] = mySum; // store original assignment
  24.   correctAwnsers[index] = a * b;
  25.   myAssignment.innerHTML = assignments[index];
  26. }
  27.  
  28. function inputHandler(evt) {
  29.   if (evt.keyCode == 13) {
  30.     if (eval(mySum) == myInput.value) {
  31.       // ntwoord goed sla op in array
  32.       awnsers[index] = myInput.value; // store input answer
  33.       console.log(awnsers);
  34.       console.log(correctAwnsers);
  35.     } else {
  36.       //antwoord fout in array
  37.       awnsers[index] = myInput.value; // store input answer
  38.       console.log(awnsers);
  39.       console.log(correctAwnsers);
  40.     }
  41.     index++;
  42.     myInput.value = "";
  43.     if (index < 10) {
  44.       init();
  45.     } else {
  46.       finished();
  47.     }
  48.  
  49.   }
  50. }
  51.  
  52. function finished() {
  53.  
  54.   document.getElementById('input').style.display = "none";
  55.  
  56.   console.log("klaar");
  57.  
  58.   var table = document.createElement('table'), tr, td, row, cell;
  59.  
  60.   for (row = 0; row < index; row++) {
  61.  
  62.     tr = document.createElement('tr');
  63.  
  64.       let td = document.createElement('td');
  65.       tr.appendChild(td);
  66.       td.innerHTML = assignments[row];
  67.  
  68.       let td1 = document.createElement('td');
  69.       tr.appendChild(td1);
  70.       td1.innerHTML = correctAwnsers[row];
  71.  
  72.       let td2 = document.createElement('td');
  73.       tr.appendChild(td2);
  74.       td2.innerHTML = awnsers[row];
  75.  
  76.       table.appendChild(tr);
  77.  
  78.     if(correctAwnsers[row] == awnsers[row]) {
  79.       tr.style.backgroundColor = "green";
  80.     }else {
  81.       tr.style.backgroundColor = "red";
  82.     }
  83.   }
  84.  
  85.   document.getElementById('container').appendChild(table);
  86.  
  87. }
  88.  
  89. myInput.addEventListener('keydown', inputHandler, false);
  90. init();
RAW Paste Data
Top