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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help using substr???



    in this code "words" is the name of an an array so "wdslast" is the final value of the array after it is scrambled (the length of which will also be random). each value in the array is an html string. what have here is my attempt to use the 32nd character from the html string and then every 48th character after that and then combine all the collected characters into one string and assign that string as the value of a variable... help??


    var words = question.sort(function() {return 0.75 - Math.random()});
    var finval = words.length; var wdslast = words[finval]; var count = wdslast.match(/img/g).length;
    var incr = 0;
    function increase() {incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;i ncr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;inc r++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr++;incr+ +;incr++;incr++;incr++;incr++;incr++;}
    var idst = wdslast.substr(32,1);
    while (count > 1){
    increase();
    var idval = idst + wdslast.substr(incr, 1); /*this part i think is my problem because I can't seem to figure out how to add the value of 'idst' to 'idval' only once and then continue as I mentioned above*/
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Can we ask what is the point of this?

    Why are you trying to increment the variable incr 32 times in steps of one? Why not incr = incr +32?

    Surely you want to use charAt(32)+ charAt(48)

    I hope you will not say that this is an attempt at a password protection script.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 09-26-2012 at 06:39 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    This code makes no sense:
    Code:
    var words = question.sort(function() {return 0.75 - Math.random()}); 
    var finval = words.length; 
    var wdslast = words[finval];
    The varialble wdslast is *guaranteed* to contain null.

    So of course everything past that point is utter hash.

    If you only want one word at random, why shuffle the array, at all?
    Code:
    var wdslast = question[ Math.floor( Math.random() * question.length ) ];
    Presto. Will actually be MORE random than the not-very-good shuffle.

    After that, the rest of it is trivial:
    Code:
    var final = "";
    for ( var c = 32, cnt = wdslast.match(/img/g).length; cnt > 0 && c < wdslast.length; c+=48, --cnt )
    {
        final += wdslast.charAt(c);
    }
    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.


  •  

    Posting Permissions

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