Computing Operators Quiz

In this example below you will see how to do a Computing Operators Quiz with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 101Computing, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 101Computing ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Computing Operators Quiz</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <center>
  <div id="quiz">
  <div id="feedback"><img src="http://www.101computing.net/wp/wp-content/uploads/cross-quiz.png" id="cross"><img src="http://www.101computing.net/wp/wp-content/uploads/tick-quiz.png" id="tick">
  </div>
  <div id="operator">+</div>
    <div id="score">
    <div id="score-label">Score:</div><div id="score-value">0/0</div></div>
  </div>
<div class="buttons">
<div class="button" onclick="javaScript:checkAnswer(1);">Arithmetic Operator</div>
<div class="button" onclick="javaScript:checkAnswer(2);">Boolean Operator</div>
<div class="button" onclick="javaScript:checkAnswer(3);">Comparison Operator</div>
</div>
  </center>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/101Computing/computing-operators-quiz-wPwLEa */
#operator {
  background-color:#B30098;
  color:#FFFFFF;
  font-size: 64pt;
  font-weight:bold;
  text-align:center;
  border-radius:50%;
  width:180px;
  height:180px;
  padding-top:30px;
  box-sizing: border-box;
  margin:10px;
}
.buttons {
  text-align: center;
}
.button {
  cursor: pointer;
  background-color:#7A0068;
  color:#FFFFFF;
  font-size: 16pt;
  font-family:Trebuchet MS;
  margin:10px;
  display: inline-block;
  padding:10px;
  border-radius: 6px;
}

.button:hover {
  background-color:#B30098;
}

#quiz {
  display:table;
}
#feedback, #operator, #score {
  display:table-cell;
}

#feedback {
  width:200px;
    text-align: center;
  vertical-align: middle;
}
#cross, #tick {
  width:120px;
  display: none;
}

#score {
  width:210px;

  font-family:Trebuchet MS;
  text-align: center;
  vertical-align: top;
}
#score-label{
  font-size: 24pt;
  padding-top:24px;
}
#score-value{
  font-size: 40pt;
}

/*Downloaded from https://www.codeseek.co/101Computing/computing-operators-quiz-wPwLEa */
var operators = ["+","-","*","/","MOD","DIV","^","AND","OR","NOT","==","!=","<","<=",">",">="];

var arithmetic = ["+","-","*","/","MOD","DIV","^"];
var boolean = ["AND","OR","NOT"];
var comparison = ["==","!=","<","<=",">",">="];

var score = 0;
var total = 0;

var operator = operators[Math.floor(Math.random()*operators.length)];

document.getElementById("operator").innerHTML = operator;
answer=0;
if (arithmetic.indexOf(operator)>=0) answer=1;
if (boolean.indexOf(operator)>=0) answer=2;
if (comparison.indexOf(operator)>=0) answer=3;

function checkAnswer(userAnswer) {
  total++;
  if (userAnswer==answer) {
    document.getElementById("tick").style.display='block';
    document.getElementById("cross").style.display='none';
    score++;
    operator = operators[Math.floor(Math.random()*operators.length)];
    document.getElementById("operator").innerHTML = operator;

    if (arithmetic.indexOf(operator)>=0) answer=1;
    if (boolean.indexOf(operator)>=0) answer=2;
    if (comparison.indexOf(operator)>=0) answer=3;
   } else {
    document.getElementById("cross").style.display='block';
    document.getElementById("tick").style.display='none';
  }
  document.getElementById("score-value").innerHTML = score+"/"+total;
  
}

Comments