Home > Blockchain >  A JavaScript Program where a number is generated and the user tries to guess it. My Issue is input
A JavaScript Program where a number is generated and the user tries to guess it. My Issue is input

Time:10-25

I have the randomizer working, My issue is getting the user's input. I can't seem to figure out how to trigger the input. I used "window.alert" as well as "console.log" but nothing happens. I'm very new to this.

This is my HTML:

<DOCTYPE html>

<html>

    <body>

        <h2> Guessing For Random Numbers! </h2>
        <p> A random number between one (1) and ten (10) will be selected. Try to guess the 
        result! Input your answer: </p>
    
    <script src= "RandomNumberMaker.js">
    </script>

    </body>
</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This is my JavaScript:

var number = window.alert("Please Enter A Number Between One and Ten: ");

var random = Math.floor(Math.random() * 10)   1;

if (random == number)
{
    document.write ("Your Guess Was Correct!");
    document.write(random);
    }else
    {
    document.write ("Your Guess Was Incorrect. The Correct Answer Was: "   random);
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use prompt to get a user input as sort of alert type

var number = window.prompt("Please Enter A Number Between One and Ten: ");

var random = Math.floor(Math.random() * 10)   1;

if (random == number) {
  document.write("Your Guess Was Correct! It is"   number);
  document.write(random);
} else {
  document.write("Your Guess Was Incorrect. The Correct Answer Was: "   random   " not "   number);
}
<h2> Guessing For Random Numbers! </h2>
<p> A random number between one (1) and ten (10) will be selected. Try to guess the result! Input your answer: </p>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related