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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2013
    Posts
    157
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Multiplying Elements in Order

    This is just an example script I made simple for the state of the argument.

    I would like to make multiplications and additions but do them in a specific order.

    For example (20 x 0.5) + 1 = (11)

    So the code I got now I tried wrapping the field_weight * 0.5 in ( ) and [ ] and it did not work.
    Code:
    <script>
    document.getElementById('do').onclick = function() {
    document.getElementById('field_weight').value = Number(document.getElementById('field_1').value) * [0.5] + [1] || 0;
    };
    </script>
    Thanks

  • #2
    Regular Coder
    Join Date
    Aug 2013
    Posts
    157
    Thanks
    23
    Thanked 0 Times in 0 Posts
    That may not have been the greatest example i just realized... but you get what I mean.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    document.getElementById('field_weight').value = 
        ((Number(document.getElementById('field_1').value) * 0.5) + 1) || 0;

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Note that [0.5] is an *ARRAY* with one element, that one element having the value of 0.5.

    JavaScript does not understand how to multiply or add arrays.
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Quote Originally Posted by glenngv View Post
    Code:
    document.getElementById('field_weight').value = 
        ((Number(document.getElementById('field_1').value) * 0.5) + 1) || 0;
    I don't get it. What is the point of the red parentheses you added there???

    Multiplication has higher precedence than addition, anyway, so that changes the arithmetic done not one iota.

    And then I really don't understand the point of the || 0 .

    The value of the expression preceding the || will never be null, so the || will never be used and thus the 0 will never be used.

    (If the user enters -2 into field_1, then Number(field_1.value) * 0.5 +1 will evaluate to zero, and that will trip the ||, but then the result is still zero, to no net effect.)

    If you think that field_1 might be non-numeric, then it needs to be tested for, separately.
    Code:
    var n = Number(document.getElementById('field_1').value);
    document.getElementById('field_weight').value = isNaN(n) ? 0 : n * 0.5 + 1;
    Last edited by Old Pedant; 10-08-2013 at 09:28 PM.
    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.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by Old Pedant View Post
    I don't get it. What is the point of the red parentheses you added there???

    Multiplication has higher precedence than addition, anyway, so that changes the arithmetic done not one iota.
    I know but it's a good practice for readability.

    If you think that field_1 might be non-numeric, then it needs to be tested for, separately.
    Code:
    var n = Number(document.getElementById('field_1').value);
    document.getElementById('field_weight').value = isNaN(n) ? 0 : n * 0.5 + 1;
    The short-circuited code (with the || 0) is the short-cut version of yours above.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Oh, yeah. Another idiotic JS quirk. NaN is considered null for purposes of the || test.

    Ugly. Wish it would just throw an error instead of getting NaN like a well-behaved language would.
    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.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Old Pedant View Post
    Ugly. Wish it would just throw an error instead of getting NaN like a well-behaved language would.
    If you want NaN to throw an error in JavaScript then it is trivially easy to add a statement to do it. The following will throw an error if the variable x is not numeric.

    if (isNaN(x)) throw new TypeError('not a number');

    So JavaScript is more flexible than those other languages because you get the choice on whether not being a number should throw an error or not.
    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.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by Old Pedant View Post
    NaN is considered null for purposes of the || test.
    No. NaN is one of the 6 "falsy" values (false, 0, "", null, NaN, undefined) in JS that is evaluated to false.


  •  

    Posting Permissions

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