Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   problem with a piece of code (http://www.velocityreviews.com/forums/t876539-problem-with-a-piece-of-code.html)

04-06-2004 09:42 AM

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>




Reply Via Newsgroup 04-06-2004 07:23 PM

Re: problem with a piece of code
 
qqq@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.


Dr John Stockton 04-06-2004 08:29 PM

Re: problem with a piece of code
 
JRS: In article <c4tu2j$1ek$1@ccserver13.unican.es>, seen in
news:comp.lang.javascript, qqq@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.


All times are GMT. The time now is 01:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57