Takes a random integer between 1 to 10, the user is then prompted to input a guess number

javascript input

2191 观看

7回复

0 作者的声誉

I am doing Javascript exercise. Here is a question: Write a JavaScript program where the program takes a random integer between 1 to 10, the user is then prompted to input a guess number. If the user input matches with guess number, the program will display a message "Good Work" otherwise display a message "Not matched"

I had this code but I do not know how to get input and display an answer.

function check() {
    var x = Math.floor((Math.random() * 10) + 1);
    var i = document.getElementById('inputFiled').value;
 
    if (i === x) {
      return "Good Work";
    }else {
       return "Not matched";
    }
   
}
<input id="inputFiled" type=text /><input type="submit" value="Submit">
<p id="random"></p>
 

作者: user4219935 的来源 发布者: 2017 年 9 月 15 日

回应 7


0

4616 作者的声誉

try this way call check function on button click and print message on p tag

function check() {
  var x = Math.floor((Math.random() * 10) + 1);
  var i = document.getElementById('inputFiled').value;

  if (i === x) {
    document.getElementById('random').innerHTML = "Good Work";
  } else {
    document.getElementById('random').innerHTML = "Not matched";
  }
}
<input id="inputFiled" type=text />
<input type="button" value="Submit" onclick="check()">
<p id="random"></p>

作者: Bhargav Chudasama 发布者: 2017 年 9 月 15 日

1

896 作者的声誉

决定

Your current version will not work as it is, since i is a string and x is an integer. You need to use == instead of === so that their types aren't compared in addition to their values.

Alternatively you could convert i to an integer when you asign it using var i = parseInt(document.getElementById('inputFiled').value);, but JavaScript will do that for free if you use the abstract comparison operator (==).

function check() {
    var x = Math.floor((Math.random() * 10) + 1);
    var i = document.getElementById('inputFiled').value;
    var feedback = document.getElementById('feedback');
 
    if (i == x) {
        feedback.innerHTML = 'Good Work';
    } else {
        feedback.innerHTML = 'Not matched';
    }
   
}
<input id="inputFiled" type=text />
<input type="button" onclick="check()" value="check" />
<p id="random"></p>
<div id="feedback"></div>
 

作者: jdgregson 发布者: 2017 年 9 月 15 日

0

1544 作者的声誉

You can do something like this;

function check() {
    var x = Math.floor((Math.random() * 10) + 1);
    var i = document.getElementById('inputFiled').value;
 
    if (i === x) {
      document.getElementById('random').innerHTML= "Good Work";
    }else {
      document.getElementById('random').innerHTML= "Not matched";
    }
   
}
<input id="inputFiled" type=text /><input type="button" value="Submit" onclick="check()">
<p id="random"></p>
 

作者: CodeHacker 发布者: 2017 年 9 月 15 日

0

1784 作者的声誉

Declare the x outside the function because every time you submit a number, a new random number is created. So generate a random number first then compare if its the correct number.

 var x = Math.floor((Math.random() * 10) + 1);
function check() {
    var i = document.getElementById('inputFiled').value;
    if (i == x) {
      document.getElementById("random").innerHTML = "Nice Guess";
    }else {
      document.getElementById("random").innerHTML = "Wrong answer.. Try again";
    }
   
}
<input id="inputFiled" type=text /><input type="submit" value="Submit" onclick="check()">
<p id="random"></p>
 
Try to input from 1-10 and you will know the difference..

作者: Shadow Fiend 发布者: 2017 年 9 月 15 日

0

1 作者的声誉

function check() {
    var x = Math.floor((Math.random() * 10) + 1);
    var i = document.getElementById('inputFiled').value;
               if (i === x) {
        alert( "Good Work");
        document.getElementById('random').innerHTML = x + "=" + i;
        }else 
          {alert("Not matched");
         document.getElementById('random').innerHTML = x + "<>" + i;
    }              
        }

<input id="inputFiled" type=text />
<input type="submit" value="Submit" onclick="return check();"/>
<p id="random"></p>
作者: Paul Tangbaoan 发布者: 2017 年 9 月 15 日

0

79 作者的声誉

Here is the modified script. Hope you can get more help from it.

 <input id="inputFiled" type=text /><input type="button" onclick="check()" value="Submit">
<p id="random"></p>

<script>
function check() {

    var x = Math.floor((Math.random() * 10));
    var i = document.getElementById('inputFiled').value;
    if (parseInt(i) === parseInt(x)) {
      alert("Good Work");
    }else {
       alert("Not matched");
    }

}
</script>

It will return you "good work" while the random number matches with user input number.

作者: Ravi gohil 发布者: 2017 年 9 月 15 日

0

6507 作者的声誉

function onClick() {
    var randomNum = Math.floor((Math.random() * 10) + 1),
        inputValue = document.querySelector('input.test').value;

    if (inputValue != "") {
        console.log(randomNum == inputValue ? 'Good Work' : 'Not matched')
    } else {
        alert('Enter any any number');
    }
}
<input class="test" type=text />
<input type="button" value="click" onclick="onClick()"/>

作者: Narendra Jadhav 发布者: 2017 年 9 月 15 日
32x32