...

View Full Version : help using substr???



thewebiphyer
09-26-2012, 05:17 PM
:confused:

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*/
}

Philip M
09-26-2012, 06:32 PM
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

Old Pedant
09-26-2012, 10:14 PM
This code makes no sense:


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?


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:


var final = "";
for ( var c = 32, cnt = wdslast.match(/img/g).length; cnt > 0 && c < wdslast.length; c+=48, --cnt )
{
final += wdslast.charAt(c);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum