![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please read the forum's rules/FAQ. Pay particular attention to the part regarding code tags.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
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> |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
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..
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Nov 2006
Posts: 4
Rep Power: 0
![]() |
use spacing for readability,
use the tab to properly indent your code blocks. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
Don't really know much about JavaScript, could you help me and show me how to do that simple function please?
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
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.
__________________
JG-Webdesign |
|
|
|
|
|
#10 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 816
Rep Power: 4
![]() |
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]++; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Start Learning Java script | smita | JavaScript and Client-Side Browser Scripting | 0 | Mar 15th, 2007 7:47 AM |
| Javascript alternative? | Writlaus | JavaScript and Client-Side Browser Scripting | 32 | Apr 21st, 2006 12:44 AM |
| HTML page not reading external Javascript file | aznluvsmc | JavaScript and Client-Side Browser Scripting | 9 | Nov 21st, 2005 9:17 PM |
| Simple Function Questions | meverha1 | C++ | 16 | Sep 12th, 2005 1:25 AM |