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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    32
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Alert result from for loop (homework)

    Hello, this is a homework project which kind of works but isn't perfect. My tutor said the result must be an alert but I ended up with a document.write instead.

    He said it was fine but I'd like to know how to make it alert the result if anyone can help.

    FWIW I tried making the contents of the for loop into a variable without success.

    Code:
    <form action="javascript: displaymessage();" method="get" />
    
    <p>Please enter a word:
    
    <input id="user_word" name="user_wrd" type="text" />
    
    <input type="submit" value="Submit" />
    
    </p>
    
    <script type="text/javascript">
    
    	function displaymessage() {
    
    		/*Set a variable for the user's word*/
    		var word_forward;
    	
    		/*Get the user's word from the form above and put it into the new variable*/
    		word_forward = document.getElementById("user_word").value;
    	
    		/*Write the user's word from the variable to the page*/
    		document.write('<p>The word is: ' + word_forward + '.</p>');
    	
    		/*Calculate the length of the user's word*/
    		var word_length=word_forward.length;
    	
    		/*Write the length of the user's word to the page*/
    		document.write('<p>The word length is: ' + word_length + ' characters.</p>');
    
    		/*Subtract 1 from word length variable to allow for character index numbers starting from zero*/
    		word_length2=(word_length-1);
    
    		/*Set a variable for the character index number*/
    		var char_num;
    
    		/*Write the first part of the result line*/
    		document.write('The word spelled backwards is: ');			
    	
    		/*Begin character index number countdown loop: loop start value / loop end value / increment*/
    		for(char_num=word_length2; char_num>=0; char_num--) {
    		
    		/*Write word backwards using charAt*/
    		document.write(word_forward.charAt(char_num) + " ");
    		
    		}
    
    	}
    
    </script>
    Last edited by Jimbolgs; 12-09-2011 at 09:03 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,139
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Make sure you declare your variable before the loop starts

    Code:
    		/*Begin character index number countdown loop: loop start value / loop end value / increment*/
    
    var backwards = "";
    for(char_num=word_length2; char_num>=0; char_num--) {
    		/*Write word backwards using charAt*/
    backwards += (word_forward.charAt(char_num) + " ");
    document.write(word_forward.charAt(char_num) + " ");
    }
    alert (backwards);
    }
    </script>
    Remember that document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes
    loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


    We are sorry that due to a typographical error we referred to Wantage MP Ed Vaisey as 'one of the top sexist MPs'. This should have read 'one of the top sexiest MPs'. Wallingford Herald.
    Last edited by Philip M; 12-09-2011 at 08:08 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Jimbolgs (12-09-2011)

  • #3
    New Coder
    Join Date
    Nov 2011
    Posts
    32
    Thanks
    15
    Thanked 0 Times in 0 Posts
    Thanks for that, but a couple of questions...

    1) What's the difference between:

    var backwards = ""; and var backwards;

    Is it simply that in the first instance you're declaring a variable and giving a null value and the second instance declaring an empty variable?

    2) What is the += for in this line:

    backwards += (word_forward.charAt(char_num) + " ");

    Thanks again.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,525
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    "" is *NOT* a null value. It *IS* a string. It's a string of zero length, but that's a hugely different thing than a null value.

    += is just shorthand.

    a += b is shorthand for a = a + b
    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.

  • Users who have thanked Old Pedant for this post:

    Jimbolgs (12-09-2011)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,139
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Jimbolgs View Post
    Thanks for that, but a couple of questions...

    1) What's the difference between:

    var backwards = ""; and var backwards;

    Is it simply that in the first instance you're declaring a variable and giving a null value and the second instance declaring an empty variable?

    var backwards = ""; defines a variable whose value is an empty string;

    var backwards; simply declares a variable whose value is undefined

    In the first case backwards += "Philip" results in the string value Philip.
    In the second case the result is undefinedPhilip.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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