PDA

View Full Version : Issue with script (loops and arrays)



yorkcoparamedic
Feb 9th, 2011, 04:11 AM
I am having an issue with this script I wrote. Actually it works the way it is now, but not the way I want it too.

This is a random raffle ticket number generater. The guys at the fire department where talking about selling 300 tickets and discussing how they were going to select and call the numbers. It got me thinking and inspired to start messing around with javascript again.

It all starts with index.html and a form w/ a button and textarea. When a user clicks the button it will generate a random number and insert it into the textarea. This is accomplished by the Generate(); function that is in Genny.js an external js file.



<h2>Raffle Ticket Number Generator</h2>
<form name="theform">
<input type="button" name="pickNumber" value="Pick a Winner" onclick="Generate();" />
<br /><br />
<textarea cols="40" rows="25" name="winnersCircle">Winning Numbers Will Display Here!
</textarea>
</form>


This much works just fine. Now onto Genny.js


//initialize counter and array
count = 1;
winners = new Array();

function Generate(){
//generate a random number from 0-300
var random_number = Math.floor(Math.random()*301);

//checks output of Math.random() against winners array to see if number has been used already
for (i = 0; i < winners.length; i++ ) {
if ( winners[i] == random_number )
alert("I am so sorry... we have accidently drawn a duplicate number! Please pick again.");
}
//add the number to the array
winners[count] = random_number;
//increment the counter
count++;
//display the stored random number
document.theform.winnersCircle.value = winners.join("\n");
}

This much works...somewhat! It generates the random number and checks the winners array to see if it is already used. If it wasn't used it adds the number to the array, increments the counter, and then displays the number in the textarea of the form, as it should. If the number was used it alerts the user, once the user clicks the button to accept the alert it carries on with the script and adds it to the array again and also displays it.
This is not what I want it to do. If the number was used I do not want it added to the array or displayed. I would also like it to regenerate another number without the alert. The alert was wrote in to assist me in checking to see if it was finding a duplicate number, thats all.

As an extra, is there a way to also add a numbered list to the results. To show the sequence of the ticket draw.

I have messed with this for days and rewrote it over and over. I have used different loops and conditional operators to try and solve this problem. But to no avail. Here I am, asking not for the answer but a push in the right direction. Any help would be great, this is becoming an obsession trying to make it work. The code above is the last attempt at this point. Everything else has returned no values or just errors.
Thanks in advance!

leapinglemur55
Feb 9th, 2011, 06:20 AM
Adding return false after the alert will stop the script after the alert is generated, but I have no idea about the rest.

Philip M
Feb 9th, 2011, 04:49 PM
Always use the braces {} with an if or else statement.

You need to add return false to terminate execution, otherwise the script continues.



//checks output of Math.random() against winners array to see if number has been used already
for (i = 0; i < winners.length; i++ ) {
if ( winners[i] == random_number ) {
alert ("I am so sorry... we have accidently drawn a duplicate number! Please pick again.");
return false;
}
}

Simply delete the alert if not required.

To sort the array into ascending order:-


winners.sort(function(a,b){return a - b});

BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.


Socialism is a philosophy of failure, the creed of ignorance, and the gospel of envy. - Sir William Churchill (1874-1965)

yorkcoparamedic
Feb 9th, 2011, 05:54 PM
Thank you very much Philip for pointing out the return false for the loop. It works great now. Here is the final code, I changed it again and used a second array to hold random_number with a ordered number prefix. That way the loop checks the numbers for doubles in the winners array and then can store the numbers for display with the prefix in the second array. By using another counter and


var dispNumber = nums + ") " + random_number;

I was able to make a numbered list for the order of the raffle ticket drawing.

Now my next question is...is there a way to loop through the entire script so that someone clicks the Pick a winner button, it does not stop if a duplicate is found and then they have to click again.
It would just find the duplicate and then regenerate another number and check it, then proceed if everything is OK. I am looking for less interaction on the user side, and also a more seemless process on the script side.


//initialize counter and array
count = 0;
winners = new Array();
//inialize counter and array for prefixed numbers. These will show the winning numbers in order by number.
nums = 1;
pre_nums = new Array();

function Generate(){
//generate a random number from 0-300
var random_number = Math.floor(Math.random()*301);

//checks output of Math.random() against winners array to see if number has been used already
for (i = 0; i < winners.length; i++ ) {
if ( winners[i] == random_number ){
alert("I am so sorry... we have accidently drawn a duplicate number! Please pick again.");
return false;
}
}
//add the number to the array
winners[count] = random_number;
//add prefixed number order to the random_number in a new variable
var dispNumber = nums + ") " + random_number;
//store prefixed number in array
pre_nums[nums] = dispNumber;
//increment the counters
count++;
nums++;
//display the stored random number
document.theform.winnersCircle.value = pre_nums.join("\n");
}


Anyone have any suggestions please?