Guest User

Untitled

a guest
Dec 28th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @author Jimmy
  3.  */
  4.  
  5. //paragraph for numbers
  6. var topPar, bottomPar;
  7.  
  8. //integers
  9. var topVal, bottomVal, resultVal;
  10.  
  11. //array to fill to 8
  12. var topArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  13. var bottomArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  14. var resultArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  15.  
  16. //string values
  17. var topString, bottomString, resultString;
  18.  
  19. //type
  20. var calculationType;
  21.  
  22. var exerciseForm;
  23.  
  24. //feedback
  25. var feedbackPar;
  26.  
  27. //score
  28. var resultPar;
  29. var goodAnswers = 0;
  30. var totalAnswers = 0;
  31.  
  32. function resetData()
  33. {
  34.     topArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  35.     bottomArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  36.     resultArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
  37. }
  38.  
  39. function getRequiredElements()
  40. {
  41.     bottomPar = document.getElementById("botBitOp");
  42.     topPar = document.getElementById("topBitOp");
  43.    
  44.     exerciseForm = document.getElementById("exerciseForm");
  45.    
  46.     feedbackPar = document.getElementById("feedbackPar");
  47.    
  48.     resultPar = document.getElementById("resultPar");
  49. }
  50.  
  51. function getRandomInt(min, max)
  52. {
  53.     return Math.floor(Math.random() * (max - min)) + min;
  54. }
  55.  
  56. function getCalculationType()
  57. {
  58.     //get required data
  59.     var calculationTypeName = document.body.getAttribute("data-bitOPType");
  60.    
  61.     switch(calculationTypeName)
  62.     {
  63.         case "and":
  64.         {
  65.             calculationType = 0;
  66.             break;
  67.         }
  68.         case "or":
  69.         {
  70.             calculationType = 1;
  71.             break;
  72.         }
  73.         default:
  74.         {
  75.             console.log("Unknown calculation string");
  76.             break;
  77.         }
  78.     }
  79. }
  80.  
  81. function createExercise()
  82. {
  83.     //get 2 random values
  84.     topVal = getRandomInt(0, 255);
  85.     bottomVal = getRandomInt(0, 255);
  86.    
  87.     //calculate result
  88.     switch(calculationType)
  89.     {
  90.         case 0:
  91.         {
  92.             resultVal = topVal & bottomVal;
  93.             break;
  94.         }
  95.         case 1:
  96.         {
  97.             resultVal = topVal | bottomVal;
  98.             break;
  99.         }
  100.         default:
  101.         {
  102.             console.log("unkown calculation type");
  103.             break;
  104.         }
  105.     }
  106.    
  107.     //parse top value to array
  108.     var tempStringTop = (+topVal).toString(2);
  109.     var topCounter = 0;
  110.     for(var i = 8 - tempStringTop.length; i < 8; i++)
  111.     {
  112.         topArray[i] = tempStringTop.substr(topCounter, 1);
  113.         topCounter++;
  114.     }
  115.    
  116.     //parse bottom value to array
  117.     var tempStringBottom = (+bottomVal).toString(2);
  118.     var bottomCounter = 0;
  119.     for(var i = 8 - tempStringBottom.length; i < 8; i++)
  120.     {
  121.         bottomArray[i] = tempStringBottom.substr(bottomCounter, 1);
  122.         bottomCounter++;
  123.     }
  124.    
  125.     //parse result value to array
  126.     var tempStringResult = (+resultVal).toString(2);
  127.     var resultCounter = 0;
  128.     for(var i = 8 - tempStringResult.length; i < 8; i++)
  129.     {
  130.         resultArray[i] = tempStringResult.substr(resultCounter, 1);
  131.         resultCounter++;
  132.     }
  133.    
  134.     //set string values
  135.     topString = topArray.join("");
  136.     bottomString = bottomArray.join("");
  137.     resultString = resultArray.join("");
  138.    
  139.     //update strings on screen
  140.     topPar.innerHTML = topString;
  141.     bottomPar.innerHTML = bottomString;
  142. }
  143.  
  144. window.onload = function() {
  145.     getRequiredElements();
  146.    
  147.     getCalculationType();
  148.     createExercise();
  149. };
  150.  
  151. function answerSubmit()
  152. {
  153.     //get input
  154.     var input = document.getElementsByTagName("input")[0].value;
  155.    
  156.     //check length
  157.     if(input.length != resultString.length)
  158.     {
  159.         feedbackPar.innerHTML = "Your answer was not the correct length.";
  160.         feedbackPar.className = "feedbackParRed";
  161.     }
  162.     else
  163.     {
  164.         //update answers
  165.         totalAnswers++;
  166.        
  167.         //check if correct
  168.         if(input == resultString)
  169.         {
  170.             goodAnswers++;
  171.            
  172.             feedbackPar.innerHTML = "Correct!";
  173.             feedbackPar.className = "feedbackParGreen";
  174.         }
  175.         else
  176.         {
  177.             feedbackPar.innerHTML = "Wrong!";
  178.             feedbackPar.className = "feedbackParRed";
  179.         }
  180.        
  181.         //update score on screen
  182.         resultPar.innerHTML = goodAnswers + " / " + totalAnswers + " Correct";
  183.        
  184.         exerciseForm.reset();
  185.     }
  186.    
  187.     //reset string arrays to all 0s, as they are not always reset in the next exercise
  188.     resetData();
  189.    
  190.     //prevent page from reloading
  191.     return false;
  192. }
  193.  
  194. function skipAnswer()
  195. {
  196.     //generate new random numbers
  197.     createExercise();
  198. }
Add Comment
Please, Sign In to add comment