CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   DOM and JSON scripting (http://www.codingforums.com/forumdisplay.php?f=15)
-   -   Hi need help with this code. javascript (http://www.codingforums.com/showthread.php?t=273070)

jolimk 09-14-2012 01:20 AM

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>

xelawho 09-14-2012 01:25 AM

error console

rdspoons 09-14-2012 02:42 AM

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>


jolimk 10-26-2012 10:15 PM

Quote:

Originally Posted by rdspoons (Post 1269813)
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 :D

felgall 10-27-2012 05:06 AM

http://javascriptexample.net/inc/prompt.gif

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.


All times are GMT +1. The time now is 06:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.