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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Check - Unsure of wrong code

    Hi there everyone,

    I check this forum quite often when I am in need to HTML of Javascript help as I am still learning. I am now follow the Missing Guide books to learn Javascript, after feeling that the Murach books were a little hard to follow.

    My questions is: I was following an example in the book, because loops are still somewhat bugging me, and I wrote down the code word for word and cannot figure out the problem with the code. I put it throught a debugger, and it was saying that the 'answer' variable was not defined, although it had been on the line before it. This is taken straight from the book, which is why I am slightly confused and having trouble.

    I appreciate any insight or assistance anyone can provide!

    Thank you

    Code:
    <script type="text/javascript">
    	
    	var score = 0; 
    	
    	var questions = [
    				["How many moons does Earth have?", 1],
    				["How many moons does Saturn have?", 31],
    				["How many moons does Venus have?", 0]
    			];
    	
    	for(var i = 0; i < questions.length; i++) {
    		askQuestion(questions[i]);
    		}
    
            function askQuestion(question) {
    		var answer = prompt(question[0]),'');
    			if(answer == question[1]) {
    				alert('Correct');
    				score++;
    				}
    				else {
    					alert("Sorry");
    					}			
    	}
    	
    	
    	
    	
    	
    	
    	</script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Learn to match up all your delimiters. You should have the same number of { as you have }, same number of ( as you have ), etc.

    Code:
    		var answer = prompt(question[0]),'');
    In that line, how many ( are there?
    How many ) are there?

    Enough said?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    By the by, you really ought to stop using that book. It is teaching you obsolete and pretty useless JavaScript. Any time you see code using alert() or prompt() or confirm() or document.write(), it is time to RUN AWAY from that code/book/tutorial.

    You would be well off to start here, instead: http://javascriptexample.net/
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder donna1's Avatar
    Join Date
    Nov 2012
    Location
    london
    Posts
    99
    Thanks
    9
    Thanked 4 Times in 4 Posts
    People keep saying that Prompt and Alert are Deprecated, and it should be done a more complicated way but no one has given me a good reason why.

    I suspect it is to keep programmers in jobs, as Prompt is easy to understand and works with simple code, can't be having that hey!

  • #5
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Learn to match up all your delimiters. You should have the same number of { as you have }, same number of ( as you have ), etc.

    Code:
    		var answer = prompt(question[0]),'');
    In that line, how many ( are there?
    How many ) are there?

    Enough said?
    Excellent! Thanks. I guess that one slipped by me. Also thank you for your reference for the website! I am definitely going to check it out. I think I started to use this book because it was one that could get me going the easiest.

    I used to do Javascript, or at least some basic javascript, but needed a kick start and it was good enough to do that. The website you gave me though seems really good and I will probably just use that from now on.

    Thanks again for your help

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by donna1 View Post
    People keep saying that Prompt and Alert are Deprecated, and it should be done a more complicated way but no one has given me a good reason why.


    in some browsers your visitor gets asked if they want to turn off JavaScript.

    Or possibly since they have been intended for debugging use only since Netscape 4 died some people may have disabled them in their browser so that they don't see the alert or prompt at all.

    Also they look completely different in different browsers so that you will never know where they will apear on the screen or what they will contain.

    In any case what's so difficult about using a <form> instead of a prompt() or an innetHTML instead of an alert()?
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    Just to second everything that Felgall said:

    Here is what the CHROME browser shows for alert( ) and prompt( ) calls:




    If the user checks that checkbox, *KABLOOEY*. He/she no longer sees ANY alert/prompt/confirms.

    Also, the alert/prompt appears in the top center of the current window and, as you can see, is really really *UGLY*.

    So if you really want CRAPPY looking web pages with ugly looking boxes that the user can easily turn off and/or where the user can just turn off JavaScript completely, then go ahead and keep using alert/prompt/confirm. If you want web pages that *WORK* and that look good, then expend a tiny modicum of effort and do things right.

    Oh, and by the by:
    but no one has given me a good reason why.
    Then clearly you haven't bothered reading the HUNDREDS of times that Felgall (and others) have explained this before. Now that you have had the message personally delivered, you can't use that excuse any more.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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