Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Jimmy
- */
- //paragraph for numbers
- var topPar, bottomPar;
- //integers
- var topVal, bottomVal, resultVal;
- //array to fill to 8
- var topArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- var bottomArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- var resultArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- //string values
- var topString, bottomString, resultString;
- //type
- var calculationType;
- var exerciseForm;
- //feedback
- var feedbackPar;
- //score
- var resultPar;
- var goodAnswers = 0;
- var totalAnswers = 0;
- function resetData()
- {
- topArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- bottomArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- resultArray = ["0", "0", "0", "0", "0", "0", "0", "0"];
- }
- function getRequiredElements()
- {
- bottomPar = document.getElementById("botBitOp");
- topPar = document.getElementById("topBitOp");
- exerciseForm = document.getElementById("exerciseForm");
- feedbackPar = document.getElementById("feedbackPar");
- resultPar = document.getElementById("resultPar");
- }
- function getRandomInt(min, max)
- {
- return Math.floor(Math.random() * (max - min)) + min;
- }
- function getCalculationType()
- {
- //get required data
- var calculationTypeName = document.body.getAttribute("data-bitOPType");
- switch(calculationTypeName)
- {
- case "and":
- {
- calculationType = 0;
- break;
- }
- case "or":
- {
- calculationType = 1;
- break;
- }
- default:
- {
- console.log("Unknown calculation string");
- break;
- }
- }
- }
- function createExercise()
- {
- //get 2 random values
- topVal = getRandomInt(0, 255);
- bottomVal = getRandomInt(0, 255);
- //calculate result
- switch(calculationType)
- {
- case 0:
- {
- resultVal = topVal & bottomVal;
- break;
- }
- case 1:
- {
- resultVal = topVal | bottomVal;
- break;
- }
- default:
- {
- console.log("unkown calculation type");
- break;
- }
- }
- //parse top value to array
- var tempStringTop = (+topVal).toString(2);
- var topCounter = 0;
- for(var i = 8 - tempStringTop.length; i < 8; i++)
- {
- topArray[i] = tempStringTop.substr(topCounter, 1);
- topCounter++;
- }
- //parse bottom value to array
- var tempStringBottom = (+bottomVal).toString(2);
- var bottomCounter = 0;
- for(var i = 8 - tempStringBottom.length; i < 8; i++)
- {
- bottomArray[i] = tempStringBottom.substr(bottomCounter, 1);
- bottomCounter++;
- }
- //parse result value to array
- var tempStringResult = (+resultVal).toString(2);
- var resultCounter = 0;
- for(var i = 8 - tempStringResult.length; i < 8; i++)
- {
- resultArray[i] = tempStringResult.substr(resultCounter, 1);
- resultCounter++;
- }
- //set string values
- topString = topArray.join("");
- bottomString = bottomArray.join("");
- resultString = resultArray.join("");
- //update strings on screen
- topPar.innerHTML = topString;
- bottomPar.innerHTML = bottomString;
- }
- window.onload = function() {
- getRequiredElements();
- getCalculationType();
- createExercise();
- };
- function answerSubmit()
- {
- //get input
- var input = document.getElementsByTagName("input")[0].value;
- //check length
- if(input.length != resultString.length)
- {
- feedbackPar.innerHTML = "Your answer was not the correct length.";
- feedbackPar.className = "feedbackParRed";
- }
- else
- {
- //update answers
- totalAnswers++;
- //check if correct
- if(input == resultString)
- {
- goodAnswers++;
- feedbackPar.innerHTML = "Correct!";
- feedbackPar.className = "feedbackParGreen";
- }
- else
- {
- feedbackPar.innerHTML = "Wrong!";
- feedbackPar.className = "feedbackParRed";
- }
- //update score on screen
- resultPar.innerHTML = goodAnswers + " / " + totalAnswers + " Correct";
- exerciseForm.reset();
- }
- //reset string arrays to all 0s, as they are not always reset in the next exercise
- resetData();
- //prevent page from reloading
- return false;
- }
- function skipAnswer()
- {
- //generate new random numbers
- createExercise();
- }
Add Comment
Please, Sign In to add comment