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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Simple Javascript program not following 'if' statements

    Wow it's been over 2 years from my last visit

    Okay I'm writing a Javascript program here:
    http://www.1728.org/test.htm

    I'm not asking for anyone to fix the whole program just some if statements.

    function twosides()
    {
    a=document.boxes.a1.value;
    b=document.boxes.b1.value;
    c=document.boxes.c1.value;
    a=a*1;b=b*1;c=c*1;
    if(a=='' || b=='' || c != ''){errswt==1;document.boxes.d1.value="Side 1 and Side 2 Must be Filled In and Side 3 Must Be Blank"};
    alert(errswt);
    if (errswt==0){alert("This Is Correct If Inputting Two Sides")}
    }

    Basically, it has 3 input boxes and I decided to put a few error messages in it. If you are solving for 2 data items and you fill in all 3 boxes, it should give you a warning. Similarly if you fill click on the "3 sides" box, and only input 2 it should also give a warning. No matter how much I reword the if statements the if statements aren't preventing the program from doing anything.
    Any suggestions?
    Thanking you in advance.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    If I tell you that '' * 1 == 0 ... will that help you find the error?

  • Users who have thanked devnull69 for this post:

    wolf1728 (10-24-2013)

  • #3
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks for the answer devnull69 but I'd like additional explanation please.

    As you probably know, JavaScript has to be the strangest programming language there is. I'm trying to check if a field is not filled in. Any other language would accept if field = "" but not JavaScript. Now I'm wondering if I should test for isNaN or if field =0 or if field != '*' etc.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by wolf1728 View Post
    Any other language would accept if field = "" but not JavaScript.
    So would JavaScript if you hadn't multiplied all of them by 1 first to convert them to numbers.

    Numbers are never == '' regardless of the language.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    wolf1728 (10-24-2013)

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,205
    Thanks
    23
    Thanked 605 Times in 604 Posts
    Code:
    errswt==1;
    S/B
    Code:
    errswt=1;
    And
    Code:
    ....Side 3 Must Be Blank"};
    S/B
    Code:
    ....Side 3 Must Be Blank";}
    Also done in at lest one other place.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • Users who have thanked sunfighter for this post:

    wolf1728 (10-24-2013)

  • #6
    New Coder
    Join Date
    Aug 2008
    Location
    Boston, MA
    Posts
    24
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks to all for the assistance. I certainly appreciate it.


  •  

    Posting Permissions

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