469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

problem with a piece of code

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[i]=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>

Jul 23 '05 #1
2 1187
qq*@ono.com wrote:
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[i]=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.

Jul 23 '05 #2
JRS: In article <c4**********@ccserver13.unican.es>, seen in
news:comp.lang.javascript, qq*@ono.com 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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by amit.purohit | last post: by
6 posts views Thread by Olaf Martens | last post: by
5 posts views Thread by Carmine Cairo | last post: by
6 posts views Thread by TPJ | last post: by
1 post views Thread by Trev | last post: by
9 posts views Thread by Jerim79 | last post: by
5 posts views Thread by Usman Jamil | last post: by
1 post views Thread by Uwe Kotyczka | last post: by
6 posts views Thread by Jai Prabhu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.