364,085 Members | 5308 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

output always equals in guessing game

2323
P: 1
im a beginner in javascript and i dont know what's wrong with my codes, its a guessing game and i wanted the output to be higher, lower and equals. the output of my codes are always equals.. i dont know what is wrong. help pls. thanks

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4.  
  5. function textNum(){
  6.  var r = Math.floor(Math.random()*10); 
  7.  var A = document.getElementById('enterno').value;
  8.  var higher = r>A;
  9.  var lower = r<A;
  10.  var equal = r=A;
  11.  
  12.  if ( r > A)
  13.  {
  14.  document.getElementById('rem').value = "higher";
  15.  }
  16.  else if (r < A)
  17.  {
  18.  document.getElementById('rem').value = "lower";
  19.  }
  20.  else (r = A)
  21.  {
  22.  document.getElementById('rem').value = "equal";
  23.  }
  24.  }
  25.  
  26. function checkbrowser() {
  27. var x = navigator
  28. alert("CodeName=" + x.appCodeName + '\n' + "Name=" + x.appName + '\n' + "Version=" + x.appVersion)
  29. }
  30. </script>
  31. </head>
  32.  
  33. <body>
  34. <table width="364" border="1">
  35.   <tr>
  36.     <td colspan ="2">Guess the Number</td>
  37.   </tr>
  38.   <tr>
  39.     <td>Enter a Number from 1 - 10:</td>
  40.     <td>
  41.       <input type="text" id="enterno" name="enterno"/>    </td>
  42.   </tr>
  43.   <tr>
  44.     <td>Check Remarks Here:</td>
  45.     <td>
  46.         <input type="text" id="rem" name="rem" readonly="readonly"/>
  47.     </td>
  48.   </tr>
  49.   <tr>
  50.     <td colspan="2"> <form>
  51.       <input type="button" value="Test The Number" onclick = "textNum()"/>
  52.       <input type="button" value="Try Again" onclick = "try()"/>
  53.       <input type="button" value="Check Browser" onclick = "checkbrowser()"/>
  54.     </form></td>
  55.   </tr>
  56. </table>
  57. </body>
  58. </html>
Feb 19 '12 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 6,604
= (line #22) is the assignment operator, not the comparison operator.
Feb 19 '12 #2

wbevan20
P: 9
Here is the fixed code:

Expand|Select|Wrap|Line Numbers
  1.     <html>
  2.     <head>
  3.     <script language="javascript">
  4.  
  5.     function textNum(){
  6.      var r = Math.floor(Math.random()*10); 
  7.      var A = document.getElementById('enterno').value;
  8.  
  9.      if ( r > A)
  10.      {
  11.      document.getElementById('rem').value = "higher";
  12.      }
  13.      else if (r < A)
  14.      {
  15.      document.getElementById('rem').value = "lower";
  16.      }
  17.      else if(r == A)
  18.      {
  19.      document.getElementById('rem').value = "equal";
  20.      }
  21.      }
  22.  
  23.     function checkbrowser() {
  24.     var x = navigator
  25.     alert("CodeName=" + x.appCodeName + '\n' + "Name=" + x.appName + '\n' + "Version=" + x.appVersion)
  26.     }
  27.     </script>
  28.     </head>
  29.  
  30.     <body>
  31.     <table width="364" border="1">
  32.       <tr>
  33.         <td colspan ="2">Guess the Number</td>
  34.       </tr>
  35.       <tr>
  36.         <td>Enter a Number from 1 - 10:</td>
  37.         <td>
  38.           <input type="text" id="enterno" name="enterno"/>    </td>
  39.       </tr>
  40.       <tr>
  41.         <td>Check Remarks Here:</td>
  42.         <td>
  43.             <input type="text" id="rem" name="rem"/>
  44.         </td>
  45.       </tr>
  46.       <tr>
  47.         <td colspan="2"> <form>
  48.           <input type="button" value="Test The Number" onclick = "textNum()"/>
  49.           <input type="button" value="Try Again" onclick = "try()"/>
  50.           <input type="button" value="Check Browser" onclick = "checkbrowser()"/>
  51.         </form></td>
  52.       </tr>
  53.     </table>
  54.     </body>
  55.     </html>
  56.  
On the last else statement you were missing an if command and the following code wasn't necessary

Expand|Select|Wrap|Line Numbers
  1. var higher = r>A;
  2. var lower = r<A;
  3. var equal = r=A;
  4.  
The above code was essentially telling the script that the random number should be equal to the input number regardless of the use of the random generator above.

Hope that helped. :)
Feb 21 '12 #3

Dormilich
Expert Mod 5K+
P: 6,604
the problem remains, on line #17 you assign A to r. you’ll run into problems if the entered value is empty.
Feb 21 '12 #4

wbevan20
P: 9
Agreed, having just looked at the code again, it should've been == instead of just =, it is still working fine with just one = even with an empty input field. Nevertheless the correct way is to have two ==, something I overlooked, my mistake.

Have now edited it to make the script correct (hopefully lol). :)
Feb 21 '12 #5

Post your reply

Help answer this question



Didn't find the answer to your JavaScript / Ajax / DHTML question?

You can also browse similar questions: JavaScript / Ajax / DHTML