0

I'm coding a simple game using JavaScript, CSS and HTML5.

The game is a simple guess the number type game in which the user would adjust the variables in which to guess the numbers from.

In the game for example the user would set the numbers to guess from i.e 0 - 5 and as he guesses a prompt would appear in the form text field showing whether or not he is closer or farther away from the correctly randomly generated number.

If the user guesses correctly a prompt will appear indicating this. The problem I am having is that when the user inputs his values for his guesses and presses the guess button the prompt appears each time showing that he guessed correctly which shouldn't be the case.

Also when the user guesses no prompt showing weather he is closer or farther away from the correct number appears in the form text field just above the guess field appears. Below is the code for my game

var my_no, count;

function load() {
  document.game.help.value = "Set range of numbers and press Start.";
}
function rnd(scale) {
  var dd = new Date();
  return
}
function range() {
  var to = 1 + 1 * document.game.to.value;
  count = 0;
  my_no = rnd(to);
  document.game.help.value = "Guess a number, enter it, and press Guess.";
}
function guess() {
  var no = document.game.number.value;
  count++;
  if (no < my_no) document.game.help.value = "My number is greater than " + no + ".";
  else if (no > my_no) document.game.help.value = "My number is less than " + no + ".";
  else alert("It took you " + count + " attempts to unlock the key of the sphinx");
}
body {
  background-image: url("egypt2.jpg");
}

* {
  font-size: 100%;
  font-family: Lithos Pro Regular;
  font-size: 20px;
}

table {
  border: 10px solid black;
  margin: 0 auto;
}
<body onLoad="load()">


  <Form name=game>
    <Table>

      <TD align=center colspan=8>Range of numbers</TD>
      <TD align=center rowspan=8><input type=button value=" Start " onclick="range()"></TD>

      <TR>
        <TD align=center>From:<br><input type="number" name=from size=30></TD>
        <TD align=center>To:<br><input type="number" name=to size=30></TD>
        </TD>

        <TR>
          <TD align=center colspan=3><input type=text name=help size=45></TD>

          <tr>
            <td align=center colspan=3>

              <input type="number" name=number size=30><input type=button value=" Guess " onclick="guess()"></TD>
          </TR>
    </table>
  </form>
  <p>
    <center>
    </center>
    <p>
</body>
Stephen P
  • 14,422
  • 2
  • 43
  • 67
seanomomom
  • 33
  • 10
  • Duplicate of http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript – mplungjan Apr 05 '17 at 16:36
  • @seanomomom Consider rolling back to the previous edit as the current answer's don't fit to the edited question any more. – le_m May 08 '17 at 23:29

1 Answers1

0

Your problem is that the variable 'my_no' is undefined because the function 'rnd' does not return anything. Assuming that you need to generate a number between 'From' and 'To' (inclusive), you should change your 'rnd' function like this:

function rnd(from, to) 
{
    /* Returns a random number between from and to (inclusive) */

    return Math.floor((Math.random() * (to - from + 1))) + from;
}

After that change, the range function should look like this:

function range() 
{
    var to = parseInt(document.game.to.value);
    var from = parseInt(document.game.from.value);

    count = 0;

    my_no = rnd(from, to);
    document.game.help.value = "Guess a number, enter it, and press Guess.";
}

Have fun unlocking the sphinx ;)

Marius Popescu
  • 274
  • 1
  • 3
  • 7
  • Thanks so much for answering the question. it never occurred to me that the range function was the problem ....im quite new to javascript as you could probably tell haha. But thanks once again! – seanomomom Apr 08 '17 at 02:11