Need Help In Writing A Specific Randomizing Function
Hi all.
I'm new to javascript. I learned C several years ago but haven't actually programmed anything for a long time.
I'm trying to create a function that will output an array of 12 strings in:
1)Random order
2)no repating numbers
3)sequential numbers
for example, these two are bad outputs for me:
"1, 2, 7, 3" etc. and also "1,5,8,1" etc.
Hope I'm clear enough. I tried using some info on the site, but couldn't find something that combines both "restrictions" and I wasn't able to fill the holes myself.
1. create a function that generates one of your four numbered units as an array
2. create a function that checks for consecutive numbers in the array--returns true if so
3. create a function that checks for repeats in the array --returns true if so
4. create a function that turns the array into a string
declare an array to hold your strings-- we'll call it ARR
create a while loop that will loop until 12 strings have been found--like
ARR = [];
var tmp;
while(ARR.length < 12){
tmp = fnc1(); //got random array
if(fnc2(tmp)){continue;} // if it was no good--try again--go to the beginning of the loop
if(fnc3(tmp)){continue;} // same as above
//if it gets here, its good
tmp = fnc(4) //convert to a string
//now check to make sure its not a duplicate
if(ARR.indexOf(tmp) == -1){
ARR.push(tmp);
}
}
There's the logic for you. Now write the functions for your homework yourself.
Bookmarks