output always equals in guessing game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 2323
    New Member
    • Feb 2012
    • 1

    output always equals in guessing game

    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

    Code:
    <html>
    <head>
    <script language="javascript">
    
    function textNum(){
     var r = Math.floor(Math.random()*10); 
     var A = document.getElementById('enterno').value;
     var higher = r>A;
     var lower = r<A;
     var equal = r=A;
     
     if ( r > A)
     {
     document.getElementById('rem').value = "higher";
     }
     else if (r < A)
     {
     document.getElementById('rem').value = "lower";
     }
     else (r = A)
     {
     document.getElementById('rem').value = "equal";
     }
     }
    
    function checkbrowser() {
    var x = navigator
    alert("CodeName=" + x.appCodeName + '\n' + "Name=" + x.appName + '\n' + "Version=" + x.appVersion)
    }
    </script>
    </head>
    
    <body>
    <table width="364" border="1">
      <tr>
        <td colspan ="2">Guess the Number</td>
      </tr>
      <tr>
        <td>Enter a Number from 1 - 10:</td>
        <td>
          <input type="text" id="enterno" name="enterno"/>    </td>
      </tr>
      <tr>
        <td>Check Remarks Here:</td>
        <td>
            <input type="text" id="rem" name="rem" readonly="readonly"/>
        </td>
      </tr>
      <tr>
        <td colspan="2"> <form>
          <input type="button" value="Test The Number" onclick = "textNum()"/>
          <input type="button" value="Try Again" onclick = "try()"/>
          <input type="button" value="Check Browser" onclick = "checkbrowser()"/>
    	</form></td>
      </tr>
    </table>
    </body>
    </html>
    Last edited by Dormilich; Feb 19 '12, 09:45 AM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    = (line #22) is the assignment operator, not the comparison operator.

    Comment

    • wbevan20
      New Member
      • Feb 2012
      • 9

      #3
      Here is the fixed code:

      Code:
          <html>
          <head>
          <script language="javascript">
           
          function textNum(){
           var r = Math.floor(Math.random()*10); 
           var A = document.getElementById('enterno').value;
           
           if ( r > A)
           {
           document.getElementById('rem').value = "higher";
           }
           else if (r < A)
           {
           document.getElementById('rem').value = "lower";
           }
           else [U][I][B]if[/B][/I][/U](r == A)
           {
           document.getElementById('rem').value = "equal";
           }
           }
           
          function checkbrowser() {
          var x = navigator
          alert("CodeName=" + x.appCodeName + '\n' + "Name=" + x.appName + '\n' + "Version=" + x.appVersion)
          }
          </script>
          </head>
           
          <body>
          <table width="364" border="1">
            <tr>
              <td colspan ="2">Guess the Number</td>
            </tr>
            <tr>
              <td>Enter a Number from 1 - 10:</td>
              <td>
                <input type="text" id="enterno" name="enterno"/>    </td>
            </tr>
            <tr>
              <td>Check Remarks Here:</td>
              <td>
                  <input type="text" id="rem" name="rem"/>
              </td>
            </tr>
            <tr>
              <td colspan="2"> <form>
                <input type="button" value="Test The Number" onclick = "textNum()"/>
                <input type="button" value="Try Again" onclick = "try()"/>
                <input type="button" value="Check Browser" onclick = "checkbrowser()"/>
              </form></td>
            </tr>
          </table>
          </body>
          </html>
      On the last else statement you were missing an if command and the following code wasn't necessary

      Code:
      var higher = r>A;
      var lower = r<A;
      var equal = r=A;
      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. :)
      Last edited by wbevan20; Feb 21 '12, 07:50 PM. Reason: Corrected slight error in code as pointed out by Dormilich

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the problem remains, on line #17 you assign A to r. you’ll run into problems if the entered value is empty.

        Comment

        • wbevan20
          New Member
          • Feb 2012
          • 9

          #5
          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). :)

          Comment

          Working...