Go Back   CodingForums.com > :: Client side development > JavaScript programming

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 02-12-2013, 09:02 PM   PM User | #1
samyouel
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
samyouel is an unknown quantity at this point
Function with if statement problem

hello guys, for my coursework i am asked to write a function named capitalCity which prompts the user for answer of "what is the capital city of france" if the answer is correct an alert box will say correct, otherwise is will say incorrect.

here is my code which i cant seem to get working

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

function capitalCity(){
var answer = prompt("what is the capital city of france?");
if (answer == "paris" || "Paris" || "PARIS"){
alert("you are correct");
} else {
alert("you are incorrect");
}
}

capitalCity();



</script>
</head>

<body>


</body>
</html>
samyouel is offline   Reply With Quote
Old 02-12-2013, 09:15 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
if ((answer == "paris") || (answer == "Paris") || (answer == "PARIS")){
alert("you are correct");
} else {
alert("you are incorrect");
}
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-12-2013, 09:16 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,102
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
if (answer == "paris" || "Paris" || "PARIS"){
should be
if ((answer =="paris") || (answer == "Paris") || answer == "PARIS")) {

But that still does not cover PaRiS etc.

Code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

function capitalCity() {
var answer = prompt("What is the capital city of France?","");
answer = answer.toLowerCase();
// it would be a good idea to strip leading and/or trailing spaces here!
if (answer == "paris") {
alert ("You are correct");
} 
else {
alert ("You are incorrect");
}
}

capitalCity();

</script>
</head>

<body>


</body>
</html>
Be aware that both prompt() and alert() are nowadays considered obsolete, and they should only be used for testing purposes.

Also be aware that the user can discover the correct answer simply with View Source in his browser.
__________________

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.

Last edited by Philip M; 02-12-2013 at 09:22 PM..
Philip M is offline   Reply With Quote
Old 02-12-2013, 09:30 PM   PM User | #4
samyouel
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
samyouel is an unknown quantity at this point
thanks guys, been a pain this all day!
samyouel is offline   Reply With Quote
Reply

Bookmarks

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 10:38 AM.


Advertisement
Log in to turn off these ads.