0

code:

<html>
<body>
<div id = 'div'>
<div id = 'div2'>
</body>
  <script>
    var rand1, rand2
    function button(){
      rand1 = Math.floor(Math.random() * 10);
      rand2 = Math.floor(Math.random() * 10);
      var html = "<input type='number' id='id'> <button onclick=' check() '> check </button> <button onclick=' again() '> again </button>" + Number(rand2) + '+' + Number(rand1);
      document.getElementById('div').innerHTML = html;
      document.write(html)
    }
    var rand3, rand4
    function again(){
      rand3 = Math.floor(Math.random() * 10);
      rand4 = Math.floor(Math.random() * 10);
      var html1 = Number(rand3) + '+' + Number(rand4);
      document.getElementById('div2').innerHTML = html1;
    }
    function check(){
      var answer = rand1 + rand2;
      var text11 = document.getElementById('id').value;
      if(answer == text11) {
        document.write('<br>' + 'correct!');
      } else {
        document.write('<br>' + 'incorrect!');
      }
    }
    button()
  </script>
</html>

This JS / HTML code is supposed to give me an equation like 1 + 1 and then if I click on again I get another one. But this gives me the error Untitled.html:20 Uncaught TypeError: Cannot set property 'innerHTML' of null. I have been researching this for half an hour and it says that I need to put the divs in front of the script. But I have done that and it still doesn't work. How can I fix this?

Luca Janss
  • 51
  • 8

0 Answers0