Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Location
    Wisconsin
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Unescaped or unknown entity

    I've been having an issue debugging this code, the else if gives me an error saying
    unescaped & or unknown entity "&&"
    so any help would be appreciated.
    Code:
    var inputNum = prompt("Please enter a number between 50 and 100:");
    	
    	if (isNaN(inputNum)) {
    	   if (inputNum.match(/one|two|three|four|five|six|seven|eight|nine|ten/))
                  {
    	   	   alert("while this is a number, it's not really a number to me.");
    	          } 
    	   else {
    	   	   alert (inputNum + " doesn't appear to be a number.");  	
    	        }
    	  }
    	else if (inputNum >= 99 && inputNum <= 51)
                  {
    		       alert("theat number, " + inputNum + ", is not between 50 and 100.");
    	          }
            
    	document.write("The user gave a number in the range! " + inputNum + "<br>");

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    I am wondering just what you are trying to debug using those prompt() and alert() debugging statements since once you remove the debugging code there isn't much left. Also the use of document.write makes it look like your target browser is Netcape 4 which is long dead.

    That error message looks like the kind of message that the HTML validator would give if you were trying to validate the script as if it were HTML. Are you sure that you have your JavaScript in a separate file from the HTML with it attached to the HTML with a script tag just before the </body> tag?

    Also && isn't the correct comparison to use as there are no numbers greater than 99 that are also less than 51.

    The following works for the comparisons that you are actually attempting without using any antiquated or debugging only calls (and miniimal other changes to your code):

    Code:
    <form><fieldset>
    <label for="num">Please enter a number between 50 and 100:</label>
    <input id="num" type="text">
    <input id="go" type="button" value="Go">
    <div id="msg"></div>
    </fieldset>
    </form>
    <script type="text/javascript">
    (function() {
    var runnum = function() {
    var inputNum = document.getElementById('num').value;
       if (isNaN(inputNum)) {
    	   if (inputNum.match(/one|two|three|four|five|six|seven|eight|nine|ten/))
                  {
    	   	   document.getElementById('msg').innerHTML = "while this is a number, it's not really a number to me.";
    	          } 
    	   else {
    	   	   document.getElementById('msg').innerHTML = inputNum + " doesn't appear to be a number.";  	
    	        }
    	  }
    	else if (inputNum >= 99 || inputNum <= 51)
                  {
    		       document.getElementById('msg').innerHTML = "that number, " + inputNum + ", is not between 50 and 100.";
    	          }
    else
    	document.getElementById('msg').innerHTML = "The user gave a number in the range! " + inputNum;
    }
    	
    document.getElementById('go').onclick = runnum;
    })();
    </script>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •