0

Excuse the bad naming of this question, i didnt know what to name it. I am basically doing this math game website. The website asks you a math question and you answer in an input box below. It creates a random number from 1 to 3, decides from multiplication, addition or substraction depending on which number it picked. On another page the user can enable or disable what ecuations they want, for example if they disable multiplication it will update a cookie and it will no longer show the user multiplication questions. But the problem is that i have only come as far as setting the cookie. Ive tried many things but non of them worked. So i dont know what to do know. I would appreciate a little bit of criticism as getting things wrong is the only time you have a chance to get things right.

if (this.value == answer) {
   if(answered >= howManyQuestions + 1){
       stop();
       stopTimer();
      }
      generate();
      document.getElementById('inputAnswer').value = '';
      answered++;
    }
}


function generate() {
  document.getElementById('questionTrack').innerHTML = answered + '/' + howManyQuestions

  switch(Math.floor(Math.random() * 3) + 1) {
    case 1:
        elementOne = Math.floor(Math.random() * 50) + 1;
        elementTwo = Math.floor(Math.random() * elementOne) + 1;
        document.getElementById('question').innerHTML = elementOne.toString() + ' + ' + elementTwo.toString();
        answer = elementOne + elementTwo;
        break;
    case 2:
        elementOne = Math.floor(Math.random() * 50) + 1;
        elementTwo = Math.floor(Math.random() * elementOne) + 1;
        document.getElementById('question').innerHTML = elementOne.toString() + ' - ' + elementTwo.toString();
        answer = elementOne - elementTwo;
        break;
    case 3:
        elementOne = Math.floor(Math.random() * 10) + 1;
        elementTwo = Math.floor(Math.random() * 10) + 1;
        document.getElementById('question').innerHTML = elementOne.toString() + ' x ' + elementTwo.toString();
        answer = elementOne * elementTwo;
        break; 
    }
}```
Isso
  • 11
  • 1
  • 1
  • 1
    Does this answer your question? [How do I create and read a value from cookie?](https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – evolutionxbox Dec 22 '21 at 23:28
  • @evolutionxbox sadly not, i need to disactivate or activate a piece of code depending on what the cookie says – Isso Dec 22 '21 at 23:33
  • use an if statement? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else – evolutionxbox Dec 22 '21 at 23:41
  • 1
    There's no way to "deactivate" a piece of code. The code for each case should check if that case is allowed by the cookie. If not, loop around and pick a new case. – Barmar Dec 22 '21 at 23:48
  • You might want to change the logic of how you select an operation. Put the allowed operations in an array, and select a random array element, instead of using a random number that might include the disallowed operations. – Barmar Dec 22 '21 at 23:49
  • @barmar ill try that. thanks – Isso Dec 23 '21 at 09:50
  • @barmar thanks! it worked out! – Isso Dec 23 '21 at 12:34

0 Answers0