1

3h of sleep and I cant figure this out... :S

I had a working something but I am changing the code a lot and removed it now I can not figure it back out. I really need help...

<!DOCTYPE html>
<html>
<body>

<p>Click the button to display a random number between 1 and 100.</p>

<button onclick="myFunction()">myFunction()</button>
<button onclick="myFunction2()">myFunction2()</button>

<p id="demo"></p>
<p id="demo2"></p>

<script>
var number = Math.floor((Math.random() * 100) + 1);

var number2 = 0;
function myFunction() {
	number2 = Math.floor((Math.random() * 100) + 1);
    var x = document.getElementById("demo")
    x.innerHTML = number2;
}
//number2 is still 0.
function myFunction2() {
    var x = document.getElementById("demo2")
    x.innerHTML = number;
}

//number is stuck at the beginning number

</script>

</body>
</html>

I need to generate new numbers from the myFunction2. How do I achieve that ?

7
  • myfunction2( ) is generating new number every time. You should check your question once again. Commented Jul 26, 2017 at 14:05
  • @shaina Where does myFunction2 generate a new number? Commented Jul 26, 2017 at 14:08
  • Source control is great spotting this kind of things! Commented Jul 26, 2017 at 14:14
  • @guest271314 Whenever I click on button myFunction2(), it gives a different number every time. Commented Jul 26, 2017 at 14:28
  • @shaina Your first comment references myFunction2, not myFunction Commented Jul 26, 2017 at 14:29

2 Answers 2

1

You are already creating random numbers from using myFunction() and it doesn't seem like a problem. In myFunction2() you don't change or really do anything so how do you expect it to keep creating random numbers? Could you make your question a bit more clear?

Edit: Just place this line inside your myFunction2() instead of where you have kept it and it should work:

var number = Math.floor((Math.random() * 100) + 1);

Enter this before you actually assign anything to var x.

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

Comments

0

number is defined once at JavaScript at Question. You can use the same approach that you did at myFunction, define number again within myFunction2

<!DOCTYPE html>
<html>

<body>

  <p>Click the button to display a random number between 1 and 100.</p>

  <button onclick="myFunction()">myFunction()</button>
  <button onclick="myFunction2()">myFunction2()</button>

  <p id="demo"></p>
  <p id="demo2"></p>

  <script>
    var number = 0;

    var number2 = 0;

    function myFunction() {
      number2 = Math.floor((Math.random() * 100) + 1);
      var x = document.getElementById("demo")
      x.innerHTML = number2;
    }

    function myFunction2() {
      number = Math.floor((Math.random() * 100) + 1)
      var x = document.getElementById("demo2")
      x.innerHTML = number;
    }

  </script>

</body>

</html>

3 Comments

Lets say I have another function that needs that random number generated in myfunction() like this function3(number2);
You need to pass number2 to function3 within myFunction call, after you have redefined number2
Within myFunction2 function3(numer2) after you have redefined number2. Note that that is not original Question, see stackoverflow.com/help/how-to-ask

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.