1

I have 1 script containing 3 functions. I have an issue were the confirmAnswer() function doesn't call the random() function.

<body>

<p>Get the random numbers...</p>
<button type="button" onclick="random()">Click Me!</button>
<p id="start"></p>

<p>Get the input...</p>
<p>Your answer is: <input type="text" id="textBox" value="">&nbsp;</p>
<p id="textbox"></p>

<p>Get the answer...</p>
<button type="button" onclick="checkAnswer()">Click Me!</button>
<p id="checker"></p>

<p>Check the answer...</p>
<button type="button" onclick="confirmAnswer()">Check Answer!</button>
<p id="checkerChecker"></p>

<script>
var number1 = Math.floor((Math.random() * 12) + 1);
var number2 = Math.floor((Math.random() * 12) + 1);
var total = number1 * number2;

function random() {
  document.getElementById("start").innerHTML = number1 + " x " + number2;
}  

function checkAnswer() {
  document.getElementById("checker").innerHTML = total;
} 

function confirmAnswer() {
    var answer = document.getElementById("textBox").value;
    var theResult;
if (answer == total) {
  theResult = "Yes, that is correct.";
  random();
  } else {
  theResult ="No, that isn't correct.";
  }
  document.getElementById("checkerChecker").innerHTML = theResult;
  }
</script>
</body>

I have used this function before, maybe it isn't the correct syntax to the original (below). Could anyone help me out?

<script>
function checkAnswer() {
    var answer = document.getElementById("textBox").value;
    var theResult;
    
    if (answer == total) {
        theResult = "Yes, that is correct.";    
        random();
    } else {
        theResult = "No, it's the wrong answer.";
        clr();
    }
    document.getElementById("checker").innerHTML = theResult;
}
</script>

The only difference I can see between both is the 2nd piece of code invokes the random() function from another script.

2
  • It's calling, successfully. Commented Jul 8, 2020 at 11:01
  • Could you add this code into the snippet please? Commented Jul 8, 2020 at 11:01

3 Answers 3

2

The issue here isn't that random() isn't running, but that it doesn't change the values of number1 and number2. You can see this by adding a console.log to the random function to show that it does run.

You could fix this by making method to rerandomise the numbers, like the below. It's also useful to use a function to calculate the total dynamically, to save having to update the total variable.

var number1;
var number2;

function randomiseNumbers()
{
    number1 = Math.floor((Math.random() * 12) + 1);
    number2 = Math.floor((Math.random() * 12) + 1);
}

randomiseNumbers();
function total(){return  number1 * number2;}

function random() 
{
  console.log("running random");
  randomiseNumbers();
  document.getElementById("start").innerHTML = number1 + " x " + number2;
}  

function checkAnswer() 
{
  document.getElementById("checker").innerHTML =  total();
} 

function confirmAnswer() 
{
    var answer = document.getElementById("textBox").value;
    var theResult;
    if (answer ==  total()) 
    {
      theResult = "Yes, that is correct.";
      random();
    } 
    else 
    {
      theResult ="No, that isn't correct.";
    }
      document.getElementById("checkerChecker").innerHTML = theResult;
}
<body>

<p>Get the random numbers...</p>
<button type="button" onclick="random()">Click Me!</button>
<p id="start"></p>

<p>Get the input...</p>
<p>Your answer is: <input type="text" id="textBox" value="">&nbsp;</p>
<p id="textbox"></p>

<p>Get the answer...</p>
<button type="button" onclick="checkAnswer()">Click Me!</button>
<p id="checker"></p>

<p>Check the answer...</p>
<button type="button" onclick="confirmAnswer()">Check Answer!</button>
<p id="checkerChecker"></p>
</body>

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect answer, problem solved. Thanks @phhu. Upvoted.
2

It does call, to make sure, please set an alert() in your random(). The problem lies in your logic. Below changes could help you

<body>
  <p>Get the random numbers...</p>
  <button type="button" onclick="random()">Click Me!</button>
  <p id="start"></p>

  <p>Get the input...</p>
  <p>Your answer is: <input type="text" id="textBox" value="" />&nbsp;</p>
  <p id="textbox"></p>

  <p>Get the answer...</p>
  <button type="button" onclick="checkAnswer()">Click Me!</button>
  <p id="checker"></p>

  <p>Check the answer...</p>
  <button type="button" onclick="confirmAnswer()">Check Answer!</button>
  <p id="checkerChecker"></p>

  <script>
    var number1, number2, total;

    function random() {
      number1 = Math.floor(Math.random() * 12 + 1);
      number2 = Math.floor(Math.random() * 12 + 1);
      document.getElementById("start").innerHTML = number1 + " x " + number2;
      alert("asdasd");
    }

    function checkAnswer() {
      total = number1 * number2;
      document.getElementById("checker").innerHTML = total;
    }

    function confirmAnswer() {
      var answer = document.getElementById("textBox").value;
      var theResult;
      if (answer == total) {
        theResult = "Yes, that is correct.";
        random();
      } else {
        theResult = "No, that isn't correct.";
      }
      document.getElementById("checkerChecker").innerHTML = theResult;
    }
  </script>
</body>

1 Comment

Thanks for the advice hgb123. The code itself still fails to work properly but I upvoted the answer as using an alert() is a very useful tool to help future me. e.g. I thought my function was failing but it was something else.
1

When you call the random function, you should also call a funtion that randomizes the variables number1 and number2 and stores the value of the number1 number2 to total,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.