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 5 of 5

Thread: need help

  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    need help

    I didnt get the whole of this code, sometimes it assign the vld_k a number, but sometimes zero. Can anyone help, what does this code do?

    <script>
    $(document).ready(function () {
    var xr = $("#auction_id");
    var zr = $("#timeStmp");

    $("#form_place_bid").bind("click", function() {
    var st = $("#sec_tok");
    var zr_str = zr.val();
    var calc_zr = zr_str.substr(zr_str.length-6);
    st.val(xr.val()*calc_zr);
    var str = xr.val();
    var str2 = st.val();
    vld_tkn(str,str2);
    });

    function vld_tkn(str,str2){
    var xa = str.substr(str.length-2)*str2.substr(str2.length-2);
    var vld_k = $("#vld_k");
    vld_k.val(xa);
    }
    });

    </script>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    And how are we supposed to guess why it works ANY part of the time without knowing what values are put into those various form fields??

    Seems to me that if a user put only NON-NUMERIC data into all those fields then of course you would never get anything because all your multiplications would fail when trying to multiply non-numbers.

    But even of ONLY numbers are involved, why would a zero result surprise you??? Ultimately you are doing this:
    Code:
    str.substr(str.length-2)*str2.substr(str2.length-2)
    (which, incidentally, is forcing numbers to be converted to strings, which is a really crappy thing to do) and if (EXAMPLE ONLY) variable str2 contains a value of 7381200 then the substr( ) will get just the last two characters ( here "00" ), convert those to a number, and multiply zero times something else. Zero times anything is always zero.
    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:

    linguist2000 (08-08-2014)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    Untested, but it feels right. And at least is more readable than a whole bunch of xr, zr, st, str, etc.
    Code:
    <script>
    $(document).ready(function () 
      {
        // let's at least use meaningful names
        var auctionID = $("#auction_id");
        var time = $("#timeStmp"); // is "timeStmp" spelled right???
        // I am going to ASSUME that your auction_id and timeStmp fields
        // contain ONLY numbers (digits) 
    
        $("#form_place_bid").bind("click", function() { 
    
            var ts_last6 = Number( time.val ) % 1000000; // gets last 6 digits of timestamp
            var sec = auctionID.val() * ts_last6;
            $("#sec_tok").val(sec);
            $("#vld_k").val( auctionID.val() % 100 * sec % 100 );
      }
    );
    </script>
    If you put the JS code at the END of the page (just before the </body> tag), where it belongs, then you could get rid of the $(document).ready() stuff.
    Last edited by Old Pedant; 08-07-2014 at 04:15 AM.
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    And if you made the simple changes below, you could also get rid of your dependency on the jQuery library.

    The below <form> and JS code actually works and seems to function correctly, though I think you need to tell us that (or not).

    I'm more than a bit worried that your <input id="sec_tok"> is intended to be some kind of SECurity TOKen. If so, that's a really bad idea. Security tokens should be generated and checked ONLY with server-side code. Never JavaScript code.
    Code:
    <html>
    <body>
    <form>
        auction id: <input id="auction_id" /><br/>
        timestamp: <input id="timeStmp" /><br/>
        sec_tok: <input id="sec_tok" readonly /><br/>
        vld_k: <input id="vld_k" readonly /><br/>
        <input type="button" id="form_place_bid" value="Bid"/>
    </form>
    
    
    <script type="text/javascript">
    var auctionID = document.getElementById("auction_id");
    var time = document.getElementById("timeStmp"); // is "timeStmp" spelled right???
    // I am going to ASSUME that your auction_id and timeStmp fields
    // contain ONLY numbers (digits) 
    
    document.getElementById("form_place_bid").onclick =
        function() { 
            var sec = Number( auctionID.value ) * ( Number( time.value ) % 1000000 ); // use last 6 of timeStmp
            document.getElementById("sec_tok").value = sec;
            document.getElementById("vld_k").value = ( Number( auctionID.value ) % 100 ) * ( sec % 100 );
        };
    </script>
    </body>
    </html>
    Last edited by Old Pedant; 08-07-2014 at 04:25 AM.
    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
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I read all explanation carefully, and I solved what the code means. Thank you very much to All of you


  •  

    Posting Permissions

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