Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-14-2012, 01:20 AM   PM User | #1
jolimk
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
jolimk is an unknown quantity at this point
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>
jolimk is offline   Reply With Quote
Old 09-14-2012, 01:25 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
error console
xelawho is offline   Reply With Quote
Old 09-14-2012, 02:42 AM   PM User | #3
rdspoons
New Coder

 
Join Date: Jun 2009
Posts: 81
Thanks: 0
Thanked 8 Times in 8 Posts
rdspoons is on a distinguished road
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>
rdspoons is offline   Reply With Quote
Users who have thanked rdspoons for this post:
jolimk (09-23-2012)
Old 10-26-2012, 10:15 PM   PM User | #4
jolimk
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
jolimk is an unknown quantity at this point
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
jolimk is offline   Reply With Quote
Old 10-27-2012, 05:06 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,451
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough


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/
felgall is offline   Reply With Quote
Reply

Bookmarks

Tags
beginners, code, javascript, user

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:41 PM.


Advertisement
Log in to turn off these ads.