I'm a super beginner in Javascript.

Asked 1 years ago, Updated 1 years ago, 39 views

Hello, I have a question
Roll the dice, roll the eyes 2x+5, and if the result is 100 or less, do 2x+5 again
If the result exceeds 100, print out the result and how many times you calculated it

I was able to generate random numbers.
But he stumbled beyond that, and it did not work very well.
Please give me some advice.
Thank you for your cooperation.

First of all, this is the sauce I wrote.

<html>
  <head>
    <metacharset="UTF-8">
  </head>
  <body>
    <script>
    functionGetRandomNumber(){
       var randnum = Math.floor(Math.random()*9);
       document.getElementById("sample").innerHTML=randnum;
    }
    // -->
    vara = 0;
    var count = 0;
    variable = randnum;

    while(randnum<100) {
      if(randnum>100){
        document.write(a+count+"th time");
      }
      a+=2*(y)+5
      ++ count;
    }
    </script>
    <p>
       <input type="button" value="dice" onclick="GetRandomNumber();">
    </p>
    <p>
       Eyes: <span id="sample">***</span>
    </p>
  </body>
</html>

javascript

2022-09-30 20:53

1 Answers

document.write is used to configure pages, so
It should start from when you press the button.
For example:

<html>
  <head>
    <metacharset="UTF-8">
    <title>Sample</title>
    <script>
    functionDice(){
       return Math.floor(Math.random()*6)+1;
    }

    functionDoIt(){
        varx = Dice();
        var count = 0;
        do{
            x = 2 * x + 5;
            ++ count;
        }while(x<=100);
        document.getElementById("result").innerHTML= 
            I calculated "+count+" times until it became x+count+" times.";
    }
    </script>
  </head>
  <body>
    <p>
       <button type="button" onclick="DoIt()">Run</button>
    </p>
    <p>
       Results: <span id="result"></span>
    </p>
  </body>
</html>


2022-09-30 20:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.