Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 1st, 2007, 4:37 PM   #1
aggelos
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 aggelos is on a distinguished road
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?
aggelos is offline   Reply With Quote
Old May 1st, 2007, 4:51 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 1st, 2007, 5:00 PM   #3
aggelos
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 aggelos is on a distinguished road
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?
aggelos is offline   Reply With Quote
Old May 1st, 2007, 5:09 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 1st, 2007, 5:28 PM   #5
aggelos
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 aggelos is on a distinguished road
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..
aggelos is offline   Reply With Quote
Old May 1st, 2007, 6:40 PM   #6
dmallord
Newbie
 
Join Date: Nov 2006
Posts: 4
Rep Power: 0 dmallord is on a distinguished road
use spacing for readability,

use the tab to properly indent your code blocks.
dmallord is offline   Reply With Quote
Old May 1st, 2007, 6:47 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old May 1st, 2007, 7:49 PM   #8
aggelos
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 aggelos is on a distinguished road
Don't really know much about JavaScript, could you help me and show me how to do that simple function please?
aggelos is offline   Reply With Quote
Old May 1st, 2007, 8:47 PM   #9
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 4 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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
Wizard1988 is offline   Reply With Quote
Old May 1st, 2007, 8:55 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:59 AM.

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