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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Else If (3 part)

    So I'm working on an else if code but it doesn't seem to work when quantity = 1

    Code:
    if (quantity != "1" && result != "1")
    		{
    		document.getElementById('result').value = quantity + " " + inunittext + "s of " + 
    
    substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText;
    		}
    		else if (quantity != "1" && result = "1")
    		{
    		document.getElementById('result').value = quantity + " " + inunittext + "s of " + 
    
    substanceAsText + " = " + result + " " + outunittext + " of " + substanceAsText;
    		}
    		else if (quantity = "1" && result != "1")
    		{
    		document.getElementById('result').value = quantity + " " + inunittext + " of " + 
    
    substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText;
    		}
    		else
    		{
    		document.getElementById('result').value = quantity + " " + inunittext + " of " + 
    
    substanceAsText + " = " + result + " " + outunittext + " of " + substanceAsText;
    		}
    Does anyone see the problem? Thanks

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts

  • #3
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'm not sure what you're trying to show me

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Ilan View Post
    I'm not sure what you're trying to show me
    Does
    Code:
    else if (quantity != "1" && result = "1")
    appear compatible with the operators shown there?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Let's be a little more specific:
    Code:
    else if (quantity != "1" && result = "1")
    Does that particular operator appear to be listed among the comparison operators?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Ilan (08-19-2012)

  • #6
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'm pretty sure that "&&" is wrong but I don't understand their explanation

    "(Logical AND) Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false."

  • #7
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Oh! It has to be ==

    Thanks!

  • #8
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    How do I add

    Code:
    toLowerCase()
    to the code so that it just shows up in lowercase?

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Add it to any *string* that you want changed.

    Code:
    document.getElementById('result').value = quantity + " " + inunittext.toLowerCase() + "s of " + 
                substanceAsText + " = " + result + " " + outunittext + "s of " + substanceAsText.toLowerCase();
    Or convert the entire string all at once:
    Code:
    document.getElementById('result').value = 
       ( quantity + " " + inunittext + "s of " + substanceAsText 
         + " = " + result + " " + outunittext + "s of " + substanceAsText ).toLowerCase();
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Ilan (08-19-2012)

  • #10
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Wow, thanks!
    I've been trying to figure out how to do that all day and now it works

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    It would have been easier if you had written your code thus:
    Code:
    // these next two lines may not be needed:
    quantity = parseInt(quantity);
    result = parseInt(result); 
    
    var text = quantity + " " 
             + inunittext + (quantity == 1 ? " of " : "s of " )
             + substanceAsText + " = " + result + " " 
             + outunittext + (result == 1 ? " of " : "s of " 
             + substanceAsText;
    
    document.getElementById('result').value = text.toLowerCase();
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    Aug 2012
    Posts
    112
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Should I change it?

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,603
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    No ... see new answer in your other thread.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    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
    •