Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   some simple JavaScript help please (http://www.programmingforums.org/showthread.php?t=13093)

aggelos May 1st, 2007 5:37 PM

some simple JavaScript help please
 
Hello, I was wondering if someone might be able to help me do something with a little program I'm testing. So this is what I've dont so far..

<html><head><title>JavaScript programming</title></head>
<body>
<script language="javascript">

document.write("<H1>Lottery number selection</H1><br> ");

alert("Hi! are you ready for your lottery numbers");

document.write("Then let us get going");

how_many=window.prompt("How many sets of number do you require","0");
temp_storage = new Array(6)
for (j=0; j<how_many; j++)
{
document.write("<br>Set ", j+1," lottery number are ");
for (i=0; i<=6; i++)
temp_storage[i]=0;//
for (i=1; i<=6; i++)
{
num = Math.random();
num=(num*48)+1;
num=Math.round(num);
switch(num)
{
case temp_storage[0]:
i--;
break;
case temp_storage[1]:
i--;
break;
case temp_storage[2]:
i--;
break;
case temp_storage[3]:
i--;
break;
case temp_storage[4]:
i--;
break;
case temp_storage[5]:
i--;
break;
default : temp_storage[i-1]=num;
}
}
for (i=0; i<6; i++)
{
num=temp_storage[i];
document.write(num, " ");
}
}
</script></body></html>

And so what I basically want to add is to see how good the random number generator is at producing random numbers. The user should input the range of integer numbers required and the number to sample it. Then i want it to display something like this:

(m) random numbers were selected between 1 and (n) – (where m and n are input)

Number No. of times selected ideal is (x) % selected ideal is (y) – (where x and y are input)

Eg.
100 random numbers were selected between 1 and 5

Number No. of times selected ideal is 20 %selected ideal is 20%
1 18 18%
2 22 22%
.
5 16 16%


Can someone help me to do this?

DaWei May 1st, 2007 5:51 PM

Please read the forum's rules/FAQ. Pay particular attention to the part regarding code tags.

aggelos May 1st, 2007 6:00 PM

Just read it. Apologies.. so...
:

<html><head><title>JavaScript programming</title></head>
<body>
<script language="javascript">

document.write("<H1>Lottery number selection</H1><br> ");

alert("Hi! are you ready for your lottery numbers");

document.write("Then let us get going");

how_many=window.prompt("How many sets of number do you require","0");
temp_storage = new Array(6)
for (j=0; j<how_many; j++)
{
document.write("<br>Set ", j+1," lottery number are ");
for (i=0; i<=6; i++)
temp_storage[i]=0;//
for (i=1; i<=6; i++)
{
num = Math.random();
num=(num*48)+1;
num=Math.round(num);
switch(num)
{
case temp_storage[0]:
i--;
break;
case temp_storage[1]:
i--;
break;
case temp_storage[2]:
i--;
break;
case temp_storage[3]:
i--;
break;
case temp_storage[4]:
i--;
break;
case temp_storage[5]:
i--;
break;
default : temp_storage[i-1]=num;
}
}
for (i=0; i<6; i++)
{
num=temp_storage[i];
document.write(num, " ");
}
}
</script></body></html>

is that right?

DaWei May 1st, 2007 6:09 PM

Hmmmmm. On many keyboards the tab key is just above the caps lock key, near the left pinky. Someone may answer your question; I can't bring myself to look at the unformatted code.

aggelos May 1st, 2007 6:28 PM

huh? why unformatted now? I put the tags in right didn't I? what to do with the tab? doesnt do anything with the tags..

dmallord May 1st, 2007 7:40 PM

use spacing for readability,

use the tab to properly indent your code blocks.

DaWei May 1st, 2007 7:47 PM

Here's an example of what we're talking about:
:

function enlarge (wURL, sp)
{
        var specs = "width=800,height=600,top=30,left=30," +
                                  "screenx=30,screeny=30," +
                                  "resizable=yes,scrollbars=yes";
        if (sp) specs = sp;
        if (globalWin)
        {
                globalWin.close ();
                globalWin = "";
        }
        popWin = open (wURL, "popWindow", specs);
        popWin.focus ();
        globalWin = popWin;
        return popWin;
}

In your code, I can't tell anything about the switch statement, other than it looks like it's probably silly.

aggelos May 1st, 2007 8:49 PM

Don't really know much about JavaScript, could you help me and show me how to do that simple function please?

Wizard1988 May 1st, 2007 9:47 PM

They are basically telling you to make indentations in your code. It really helps the readability.:)

Regarding learning javascript:
http://www.w3schools.com/js/default.asp
Functions in JavaScript

I hope this helps.

The Dark May 1st, 2007 9:55 PM

The problem with your formatting is that you cut and paste from your post into the code tags, but the formatting has already been lost by that point. It is much better to cut and past your original code into code tabs, so that we can follow the logic, by looking at the indenting. At the moment, the best way to view it is to view the HTML source of this page and look at the code as HTML, which is more work that a lot of people will want to do.

As to how to do the program, one way would be to:
- get the range (n)
- get the count (m)
- allocate an array of counts of size n+1 (since arrays are zero based) - you already have most of this in temp_storage
- loop for j = 0 ; j < m; j++ (you already have this)
- get a random number between 1 and n (you have this except for the hard coded 48) - call this num
- instead of the switch you have now, just increment the temp_storage array entry for that number:
:

temp_storage[num]++;
- then just run through the array to print out the results


All times are GMT -5. The time now is 2:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC