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 to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Hi need help with this code. javascript

    This is my code i dont get it to work, i want it to ask the user how is he and depending on the answer alert something, and call the user by the name.

    <HTML>
    <body>
    <script type="text/javascript">

    var name=prompt("Hi. Whats your name?")

    var mood=prompt("Hi " + name + ". How are you?")
    var fine="fine"
    var bad="bad"

    if mood==fine
    alert("Im glad you are fine, " + name)

    if mood=bad
    alert("Ooo. Thats bad, Im sorry. I hope you get better, " + name)


    </body>
    </SCRIPT>
    </HTML>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,930
    Thanks
    56
    Thanked 550 Times in 547 Posts
    error console

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    81
    Thanks
    0
    Thanked 8 Times in 8 Posts
    This may help. You can compare the code and the comments to your original code.
    Code:
    <HTML>
    <body>
    <script type="text/javascript">
    //You may want to add your own default word for the prompt to 
    //avoid the prompt's 'undefined' default.
    var name=prompt("Hi. Whats your name?","friend")
    
    var mood=prompt("Hi " + name + ". How are you?") //'undefined' is the default answer
    var fine="fine"
    var bad="bad"
    
    //remember to put your conditional comparisons inside of parentheses.
    if(mood==fine)
    	alert("Im glad you are fine, " + name)
    //you can use an if..else if.. else struture to allow a default 
    //answer if neither 'fine' nor 'bad' are given as the response.
    //dont' forget mood=bad is an asignment, not a comparison.
    else if(mood==bad)
    	alert("Ooo. Thats bad, Im sorry. I hope you get better, " + name)
    //and maybe add a default greeting in case 'fine' or 'bad' are not entered
    else
    	alert("So..., it looks like you're in a " + mood +" mood, " + name)
    
    //And don't forget to close the script tag
    </script>
    
    </body>
    </SCRIPT>
    </HTML>

  • Users who have thanked rdspoons for this post:

    jolimk (09-23-2012)

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rdspoons View Post
    This may help. You can compare the code and the comments to your original code.
    Code:
    <HTML>
    <body>
    <script type="text/javascript">
    //You may want to add your own default word for the prompt to 
    //avoid the prompt's 'undefined' default.
    var name=prompt("Hi. Whats your name?","friend")
    
    var mood=prompt("Hi " + name + ". How are you?") //'undefined' is the default answer
    var fine="fine"
    var bad="bad"
    
    //remember to put your conditional comparisons inside of parentheses.
    if(mood==fine)
    	alert("Im glad you are fine, " + name)
    //you can use an if..else if.. else struture to allow a default 
    //answer if neither 'fine' nor 'bad' are given as the response.
    //dont' forget mood=bad is an asignment, not a comparison.
    else if(mood==bad)
    	alert("Ooo. Thats bad, Im sorry. I hope you get better, " + name)
    //and maybe add a default greeting in case 'fine' or 'bad' are not entered
    else
    	alert("So..., it looks like you're in a " + mood +" mood, " + name)
    
    //And don't forget to close the script tag
    </script>
    
    </body>
    </SCRIPT>
    </HTML>
    Thanks actually worked thank you very mcuh youre such a pro

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,631
    Thanks
    0
    Thanked 648 Times in 638 Posts


    What do you expect to happen when your visitor checks the checkbox to turn off JavaScript (as you can see appears in some browsers as shown above - since prompt is intended for debugging use only)? If that is selected then no value gets returned from the prompt. You'd do better to use a form in the web page if you want visitors to be able to enter something.
    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
    •