problem with a piece of code

G

Guest

hello,

I have a problem with this piece of code. I want to open a random web page
each time and remember which one has been opened before because I don´t want
to repeat. It works fine the first 3 or 4 times, but after that, it gets
collapsed and i have to close the window. Any advice?

<script language="javascript">
// numero de ejercicios que componen todo. all the exercises
var numero_de_ejercicios=10;
// ejercicios ya salidos showed exercises
var ej_salidos=new Array(numero_de_ejercicios);

for (var i=0; i<ej_salidos.length;i++)
{ ej_salidos=0;}


function ej_aleat(maximo,salidos){
var azar=100;
var ej_salido=0;

// repite mientras salga el 0, uno mayor que no exista o uno repetido
// repites while num_ej=0 (that page doesn´t exists), num_ej greater than
max number of exercise o i get one repeated

do{
var num_ej=Math.floor(azar*Math.random());
if(salidos[num_ej]==1)
{ej_salido=1}
}
while(((num_ej>maximo)||(num_ej==0))||(ej_salido==1));

if(num_ej<100){
var dir='ejercicio00'+num_ej+'.htm'}
else if (num_ej>99){
var dir='ejercicio'+num_ej+'.htm'}
else{
var dir='ejercicio0'+num_ej+'.htm'}

window.open(dir);
ej_salidos[num_ej]=1;
}
</script>
<h3 class="ejercicio"><a href="#"
onClick='ej_aleat(numero_de_ejercicios,ej_salidos);'>Ejercicios
aleatorios.</a> </h3>
 
R

Reply Via Newsgroup

hello,

I have a problem with this piece of code. I want to open a random web page
each time and remember which one has been opened before because I don´t want
to repeat. It works fine the first 3 or 4 times, but after that, it gets
collapsed and i have to close the window. Any advice?

<script language="javascript">
// numero de ejercicios que componen todo. all the exercises
var numero_de_ejercicios=10;
// ejercicios ya salidos showed exercises
var ej_salidos=new Array(numero_de_ejercicios);

for (var i=0; i<ej_salidos.length;i++)
{ ej_salidos=0;}


function ej_aleat(maximo,salidos){
var azar=100;
var ej_salido=0;

// repite mientras salga el 0, uno mayor que no exista o uno repetido
// repites while num_ej=0 (that page doesn´t exists), num_ej greater than
max number of exercise o i get one repeated

do{
var num_ej=Math.floor(azar*Math.random());
if(salidos[num_ej]==1)
{ej_salido=1}
}
while(((num_ej>maximo)||(num_ej==0))||(ej_salido==1));

if(num_ej<100){
var dir='ejercicio00'+num_ej+'.htm'}
else if (num_ej>99){
var dir='ejercicio'+num_ej+'.htm'}
else{
var dir='ejercicio0'+num_ej+'.htm'}

window.open(dir);
ej_salidos[num_ej]=1;
}
</script>
<h3 class="ejercicio"><a href="#"
onClick='ej_aleat(numero_de_ejercicios,ej_salidos);'>Ejercicios
aleatorios.</a> </h3>


You say it works the first three or four times - In Part, from what I
can see above, its because you have only three url's defined in the
script... Secondly, you don't store any previous selection anywhere...

I think you're better off having your url's written in to an array - then...

Step 1: Randomize between zero and the maximum size of the array.

Step 2: Once you have a random value - check for a previously set cookie
- if there is a previous cookie, then compare its value to the random
number - If they are equal, return to step 1.

Step 3: Display your page.

Something like the above will work of course only if cookies is switched
on - otherwise there is no way for the randomizer to know what previous
number was selected hence the chances for duplicaity exist.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript, (e-mail address removed) posted at Tue, 6 Apr 2004
11:42:46 :
I have a problem with this piece of code. I want to open a random web page
each time and remember which one has been opened before because I don´t want
to repeat. It works fine the first 3 or 4 times, but after that, it gets
collapsed and i have to close the window. Any advice?

Let the number of pages be N. Deal the numbers 0 - (N-1) into an array
in random order (See FAQ; see <URL:http://www.merlyn.demon.co.uk/js-
randm.htm#SDFS>). Go through these numbers in turn to index the pages.

Indent your code to show structure. Put spaces in it for readability.

Do not allow posting to News to like-wrap it; what you post should be
directly executable by copy'n'paste.

Use a routine to extend num_ej to a three-digit string, then
var dir = 'ejercicio' + num_ej3 + '.htm'; FAQ 4.6 Stretch, perhaps
changed for c=" " and L=3.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top