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
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    really basic if else problem

    ok so i just started learning javascript and i know next to nothing and i wasnt surprised when what i wrote didnt work. so i wonder if you guys can help me figure out whats the problem.

    <html>
    <head>
    <script>
    var a= prompt();
    if (var a>1)
    {
    alert("I hate 1s!");
    }
    else
    {
    alert("I like you!");
    }
    </script>
    </head>

    <body>


    </body>
    </html>


    so why doesnt this work?
    thanks for the help
    james.

  • #2
    Regular Coder
    Join Date
    Jun 2011
    Posts
    103
    Thanks
    0
    Thanked 13 Times in 13 Posts
    number one because the prompt() function takes two parameters and returns a string, not an interger
    Also in your script tag, you need to specify what type the script is for the parser

    Code:
    <head>
    <script type="text/javascript">
    <!--
    function prompter() {
    var reply = prompt("Hey there, good looking stranger!  What's your name?", "")
    alert ( "Nice to see you around these parts " + reply + "!")
    }
    //-->
    </script>
    </head>
    <body>
    <input type="button" onclick="prompter()" value="Say my name!">
    </body>

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thank you for you code, but so can I actually make it ask me hows my day and when i reply good or bad to a prompt, it displays the relevant message?

    so heres what i got:

    <html>
    <head>
    <script>
    function prompter() {
    var a= prompt(" How are are today? good? bad?")
    if (var a= good)
    {
    alert("Thats nice to hear!");
    }
    else
    {
    alert("I am sorry, hope you enjoy the rest of your day!");
    }
    }
    </script>
    </head>

    <body>
    <input type="button" onclick="prompter()" value="How are you today, Sir?">

    </body>
    </html>


    is it possible that this can work that way, or am i just barking up the wrong tree?
    thanks!

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    You should start learning the basics

    1. Prompt returns a string. String literals have to be enclosed in single or double quotes
    2. The compare operator is == and not =
    3. if you want to compare an existing variable you must not use the var keyword
    Code:
    if(a=="good")

  • Users who have thanked devnull69 for this post:

    hade911 (07-03-2011)

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    oh thank you so much we were learning this at school and i kinda forgot! thanks that cleared it all up!


  •  

    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
    •