CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Any suggestions?

    Code:
    <html>
    <body>
    <script = "text/javascript">
    
    //
    var showTime = 8;
    var showTime2 = 12;
    var showTime3 = 15;
    var ES = "";        // literal empty string
    var BR = "<br/>";   //HTML line break
    var movie = prompt("What movie do you want to see?");
    
        //Calculate
    
    
        If (showTime==1pm + 5pm){
    
    
           document.write("Your ticket price is: "" for showTime + "$" + BR.);
    
           }
          else if (showTime2==11pm){
                 document.write("Your ticket price is: "" + for showTime2 + "$" + BR.);
                 }
    
           else if (showTime3==3pm-8pm){
                document.write("Your ticket price is: ""  + for showTime3 + "$" + BR.);
    
           }
           else
           prompt("Invalid showtime");
           }
    
    
           //Calculate
    
    
    </script>
    </body>
    </html>
    Last edited by PeejAvery; November 15th, 2011 at 02:32 PM. Reason: Added code tags

  2. #2
    Join Date
    Jun 2009
    Posts
    113

    Re: Any suggestions?

    Lots of problems there.
    1. You've defined a variable ES which you don't then use anywhere:
    var ES = ""; // literal empty string
    2. You're putting up a prompt asking for what movie you want to see, and assigning the result to a variable called "movie", but you're not checking the value of "movie" elsewhere in your script, instead you're checking the value of the "ShowTime"s.
    3. When you are checking the value, you're not comparing the value as a string. You need to put quotes around the values you're testing for.
    4. Your first "if" has a capital "I"; JavaScript is case sensitive.
    So the first "if" statement should look like this:
    if (showTime== "1pm - 5pm"){

    5. You're concatenation of the document.write contents is very messy and has syntax errors. Here is how it should look:
    document.write("Your ticket price is: $" + showTime + " for " + movie + BR);
    Which will add together
    "Your ticket price is: $"
    along with the value for showTime, which you defined as "8", then the word " for " (with the spaces), followed by the value they entered for movie - which MUST be the exact text "1pm - 5pm" otherwise the test condition won't be met.
    6. Your final "else" test has a closing curly bracket after the prompt, but no corresponding opening one.
    7. Don't use "prompt" at the end, use "alert" instead; "prompt" is expecting an input whilst alert is a simple message dialog.
    8. Personally I would use a switch...case instead of separate if, else if, else statements. Read up on this at W3 schools which is an excellent teaching site for JavaScript: http://www.w3schools.com/js/js_switch.asp

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured