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
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with this string

    I can't get this to work. It asks me for all the inputs then after the last input the webpage can't load.

    var adjective;
    var location;
    var firstName;
    var color;

    adjective = prompt('Enter a adjective: ');
    location = prompt('Enter a location: ');
    firstName = prompt('Whats your name: ');
    color = prompt('Whats your favorite color: ');

    document.write('Last night I visited the ' + adjective + ' restaurant I have ever been to.');
    document.write('\nIt was located right in the middle of an ' + location + ' just outside of town.');
    document.write('\nThe name of the place, \"' + firstName + '\'s, \" was lit up with big ' + color + ' lights.');

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    location is a reserved word and may not be used as the name of a variable.

    The HTML newline character is <br> not \n.

    Code:
    <script type = "text/javascript">
    
    var adjective = prompt("Enter a adjective: ","");
    var locn = prompt("Enter a location: ","");
    var firstName = prompt("What\'s your name: ","");
    var color = prompt("What\'s your favorite color: ","");
    
    document.write('Last night I visited the ' + adjective + ' restaurant I have ever been to.');
    document.write('<br>It was located right in the middle of an ' + locn+ ' just outside of town.');
    document.write('<br>The name of the place, \"' + firstName + '\'s,\" was lit up with big ' + color + ' lights.'); 
    
    </script>
    prompt() and document write() have long been obsolete, and should be used (if at all) only for testing and debugging purposes. Use DOM methods to get input from and display messages to your users.

    Note how to escape an apostrophe.

    I expect you can see how this script does not attempt to ensure that the user entries make sense, or indeed require any entry at all. If so you have learned something important - input validation is often 80% of a practical script.

    Please do not make any more posts asking us to do your homework for you. Pretending that other people's work is your own is normally regarded as cheating. Dishonest, in fact.


    I suppose that what we are all waiting for is a picture of Kate with the baby coming out. - ITV Reporter
    Last edited by Philip M; 09-23-2013 at 06:31 PM.

    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
    •