473,386 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1298
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: amit.purohit | last post by:
hi, I have a very strange problem on my login Page. the Page was working fine a few days back, but now does not generate post back events for controls. this login page uses form based...
6
by: Olaf Martens | last post by:
Greetings! Please consider the following piece of program code (note that I have stripped quite a lot of code here): int foo(void) { unsigned short l_valbuf; // address of this goes to...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
by: Trev | last post by:
Hi, I'm hoping that someone in this group can shed some light on an issue I'm having with a Formview. I have a Web User Control (.ascx) with a Formview. The Formview contains 2 Multiviews,...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
1
by: Uwe Kotyczka | last post by:
Hallo, sorry for multiposting, but I am really looking for some hint to solve my problem. And no, I don't use Matlab, but maybe the matlab people have an idea nevertheless. I have to solve a...
6
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.