problem with a piece of code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    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="javas cript">
    // numero de ejercicios que componen todo. all the exercises
    var numero_de_ejerc icios=10;
    // ejercicios ya salidos showed exercises
    var ej_salidos=new Array(numero_de _ejercicios);

    for (var i=0; i<ej_salidos.le ngth;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.flo or(azar*Math.ra ndom());
    if(salidos[num_ej]==1)
    {ej_salido=1}
    }
    while(((num_ej> maximo)||(num_e j==0))||(ej_sal ido==1));

    if(num_ej<100){
    var dir='ejercicio0 0'+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="ejercici o"><a href="#"
    onClick='ej_ale at(numero_de_ej ercicios,ej_sal idos);'>Ejercic ios
    aleatorios.</a> </h3>



  • Reply Via Newsgroup

    #2
    Re: problem with a piece of code

    qqq@ono.com wrote:
    [color=blue]
    > 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="javas cript">
    > // numero de ejercicios que componen todo. all the exercises
    > var numero_de_ejerc icios=10;
    > // ejercicios ya salidos showed exercises
    > var ej_salidos=new Array(numero_de _ejercicios);
    >
    > for (var i=0; i<ej_salidos.le ngth;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.flo or(azar*Math.ra ndom());
    > if(salidos[num_ej]==1)
    > {ej_salido=1}
    > }
    > while(((num_ej> maximo)||(num_e j==0))||(ej_sal ido==1));
    >
    > if(num_ej<100){
    > var dir='ejercicio0 0'+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="ejercici o"><a href="#"
    > onClick='ej_ale at(numero_de_ej ercicios,ej_sal idos);'>Ejercic ios
    > aleatorios.</a> </h3>
    >
    >
    >[/color]

    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.

    Comment

    • Dr John Stockton

      #3
      Re: problem with a piece of code

      JRS: In article <c4tu2j$1ek$1@c cserver13.unica n.es>, seen in
      news:comp.lang. javascript, qqq@ono.com posted at Tue, 6 Apr 2004
      11:42:46 :
      [color=blue]
      >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?[/color]

      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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...