problem with a piece of code

Discussion in 'Javascript' started by Guest, Apr 6, 2004.

  1. Guest

    Guest 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>
     
    Guest, Apr 6, 2004
    #1
    1. Advertising

  2. 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=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.
     
    Reply Via Newsgroup, Apr 6, 2004
    #2
    1. Advertising

  3. JRS: In article <c4tu2j$1ek$>, seen in
    news:comp.lang.javascript, 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.
     
    Dr John Stockton, Apr 6, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Przemo
    Replies:
    5
    Views:
    534
    Teemu Keiski
    Jul 18, 2004
  2. Replies:
    6
    Views:
    424
    Keith Thompson
    May 17, 2006
  3. Jaco Naude
    Replies:
    9
    Views:
    777
  4. Alexander

    What is the problem with this piece of code?

    Alexander, Sep 17, 2010, in forum: C Programming
    Replies:
    7
    Views:
    337
    Keith Thompson
    Sep 20, 2010
  5. Patrick Plattes

    Download a file piece by piece

    Patrick Plattes, Nov 30, 2006, in forum: Ruby
    Replies:
    2
    Views:
    209
    Patrick Plattes
    Nov 30, 2006
Loading...

Share This Page