JSFiddle - React, Tailwind, and code Playground
HTML
<p id="first"></p>
<p id="operator"></p>
<p id="second"></p>
<p>=</p>
<input id="entry"></input>
<button onclick="calc()">
Validate
</button>
JavaScript
/* Build some random numbers between 1 and 10 */
var first = Math.floor((Math.random() * 10) + 1);
var second = Math.floor((Math.random() * 10) + 1);
var operator = ['+', '-', '*', '/'];
/* Get a random operator */
var index = Math.floor(Math.random() * 4);
var operator = operator[index];
/* Render HTML */
document.getElementById('first').innerHTML = first;
document.getElementById('operator').innerHTML = operator;
document.getElementById('second').innerHTML = second;
function calc(p1, p2) {
/* Fetch user input */
var entry = document.getElementById('entry').value;
/* Calc result */
var result = "";
switch (index) {
case 0:
result = first + second;
break;
case 1:
result = first - second;
break;
case 2:
result = first * second;
break;
case 3:
result = first / second;
break;
}
/* Validate result */
if (entry == Math.round(result * 100) / 100) {
alert("Richtig!")
} else {
alert("Falsch!")
}
}