473,511 Members | 15,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with JavaScript math.round etc

16 New Member
Hi guys im doing a course which includes some JavaScript and I need help with the following question please,

Edit M150_TMA05_2008J_Q1_task01.html replacing the existing
JavaScript with the function,
getRoundedRandomNumber(aNumber). This function should
return whole numbers between 0 and aNumber, using
Math.round() as outlined above.
Write a comment to document what this function does, using an
extended comment (that is, one between /* and */). Also
provide appropriate single-line comments (the ones starting
with //) in the code, describing how it works. Paste a copy of
the getRoundedRandomNumber() function into your Solution
Document.
Also remove the line of code and associated comment you wrote
for part (ii).
2
Read the ONCLICK event handler for the file’s HTML form to
understand how getRoundedRandomNumber() is invoked and
how the returned number is displayed.
The code I have so far is below but I don't think its right please help as I need to complete this part to move onto the next.

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>
  4.  
  5. <SCRIPT language = "JavaScript">
  6.  
  7.         //generate a random number greater than or equal to 0.0 and less than 50.0 
  8.         //rounded to a whole number
  9.         //and display it in an alert dialogue
  10.  
  11. var getRoundedRandomNumber = Math.round(Math.random()*49);
  12.  
  13. window.alert("Random number returned is: " + getRoundedRandomNumber);
  14.  
  15.  
  16. //generate a random number and round down
  17.  
  18. var getRandomNumber = Math.floor(Math.random()*49)
  19.  
  20. </SCRIPT>
  21. </HEAD>
  22. <BODY>
  23. <STRONG>A test of the random number functions <BR></STRONG>
  24.     <FORM NAME = "randomForm">
  25.  
  26.     <INPUT TYPE = "button" NAME = "randomButton"  VALUE ="Display Random Number!"
  27.             ONCLICK = "window.alert(getRandomNumber);">
  28.     </FORM>
  29. </BODY>
  30. </HTML>
May 6 '09 #1
9 5585
Dormilich
8,658 Recognized Expert Moderator Expert
the first thing you need is a function (see description) where you need to put in the code, so you can call it onclick.

further, your form needs an action attribute. and the script a type attribute.
Expand|Select|Wrap|Line Numbers
  1. <form action="name_of_page" … >
  2.  
  3. <script type="text/javascript>
May 7 '09 #2
DaiOz
16 New Member
Hi, any chance of an example of what function code you would use, I'm pretty useless but trying to learn lol
May 7 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
the task requires you to write
Expand|Select|Wrap|Line Numbers
  1. function getRoundedRandomNumber(aNumber)
  2. {
  3.   // put your code here
  4.  
  5.   return roundedNumber;
  6. }
and then write some code to call this function onclick.

hint: the basic idea is outlined in the task
May 7 '09 #4
DaiOz
16 New Member
My attempt was as follows;

Expand|Select|Wrap|Line Numbers
  1. Function getroundedrandomnumber(aNumber)
  2. {
  3. var aNumber = Math.floor(Math.random()*49);
  4.  
  5. return aNumber;
  6. }
Help please lol
May 7 '09 #5
acoder
16,027 Recognized Expert Moderator MVP
JavaScript is case-sensitive, so make sure the case is absolutely correct and also look at the error console.
May 7 '09 #6
DaiOz
16 New Member
I'm typing on my iPhone in work and using notepad to right the script lol
May 7 '09 #7
acoder
16,027 Recognized Expert Moderator MVP
But you must be testing it in a browser, no?
May 7 '09 #8
DaiOz
16 New Member
Ah i see what you mean it says 'aNumber' is undefined
May 7 '09 #9
DaiOz
16 New Member
Thanks for the help guys I don't know how but I made it work the error console tip was great will post code when I get home, cheers again
May 7 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
3310
by: Michael Hagstrom | last post by:
Good Day - I have written a form which collects input for a fare calculation. The form collects the following: 1) Departure or Destination Group (1,2,or 3) 2) Number of Adults 3) Number of...
2
9494
by: gene | last post by:
I have some javascript code that displays random images in random places on the page by making changes to the document's css. This works in Safari and IE, but I can't get it to work on Mozilla. ...
8
1482
by: Cat | last post by:
Greetings, I have an old javascript effect that was running on some of my sites for quite long, working fine in Netscape (up to 4.75) and IE. It still works in the newer IEs, but not in the new...
8
2509
by: JS | last post by:
I've managed to create a simple script to convert between metric and imperial. It works for CMS to INCHES and vice versa but not for KGS to STONES/POUNDS. Can anyone shed any light on this? ...
1
1772
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone...
2
5397
by: Head In A Pan | last post by:
Hello! My JavaScripting is at novice level - and after completing a tutorial on a 'floating layer' I was proud that I got it working on Firefox, Safari, Camino & even IE5 for mac!!! But not IE7!...
4
1779
by: Yonih | last post by:
So I am trying to get this Calculator to work. It needs to take in a vaule, and select a shipping Everythin works great except the shipping part. I need it to take the shipping value and add it to...
1
1211
Bugsy1937
by: Bugsy1937 | last post by:
Hello Folks, I wrote the following long ago and it worked for years. It Still works in IE, but can't get it to work in Firefox3. Here's the script, hope someone can help me: <html> <body>...
0
7245
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7085
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1577
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.