Results 1 to 7 of 7

Thread: return 0 if another variable is 0

  1. #1
    Join Date
    Jan 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default return 0 if another variable is 0

    Howdy Folks,

    I'm trying to have var x equal zero if var y equals 0.

    if var y is greater than 0 than war x will equal 240

    This is the mess that I've coded. Novice level one I'm fully aware, any help or a direction would be great.


    Code:
    <script type="text/javascript">
    var y = 15;
    var x = wagemin;
    
    wagemin()
    {
    
       if (wth <= 0) 
       {
       return 0;
       }
       else
       {
       return 240;}
    }
    document.write(x)
    </script>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    var y = 15,
        x = y && 240;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    Ummm, I think you meant to say
    Code:
    var y = 15,
        x = wth && 240;

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, I was going by the description rather than the provided code. I'm guessing the mysterious wth variable was an error.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    wth is undefined. Twey's code is right, except for negative values of y. This would fit the desired outcome stated by the OP about as well as can be divined:

    Code:
    var y = 15,
        x = Math.max(0, y) && 240;
    I'm trying to have var x equal zero if var y equals 0.

    if var y is greater than 0 than war x will equal 240
    It is left to the imagination what should happen if y is less than zero, but the pseudo code seems to indicate that x should be zero in that case. But negative values of y may be impossible, or the desired outcome for x in such a case may still be 240. If either of those cases are true, Twey's elegantly simple code is all that's required.

    However, it's possible that yet another outcome is wanted for negative y.
    Last edited by jscheuer1; 12-09-2008 at 04:30 PM. Reason: add another possibility for -y
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    primitive version (hehe- just wanted to show a different way):

    Code:
    var y = 0, x;
    if(y <= 0) x = 0;
    else if(y > 0) x = 240;
    -magicyte

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by ChrisVersion2 View Post
    I'm trying to have var x equal zero if var y equals 0.

    if var y is greater than 0 than war x will equal 240

    This is the mess that I've coded. Novice level one I'm fully aware, any help or a direction would be great.
    Hopefully all of us have already provided enough code examples to get you on your way, but if not - please answer:

    What if y is less than zero, what then? And what's war x?

    If you already have your answer, let us know that too, so we can close this thread.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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