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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,894
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    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.
    Last edited by Philip M; 02-12-2013 at 09:22 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.

  • #4
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys, been a pain this all day!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •