469,307 Members | 2,110 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,307 developers. It's quick & easy.

Help with JavaScript math.round etc

16
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 files 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 5285
Dormilich
8,651 Expert Mod 8TB
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
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,651 Expert Mod 8TB
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
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 Expert Mod 8TB
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
I'm typing on my iPhone in work and using notepad to right the script lol
May 7 '09 #7
acoder
16,027 Expert Mod 8TB
But you must be testing it in a browser, no?
May 7 '09 #8
DaiOz
16
Ah i see what you mean it says 'aNumber' is undefined
May 7 '09 #9
DaiOz
16
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

Post your reply

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

Similar topics

5 posts views Thread by Michael Hagstrom | last post: by
4 posts views Thread by Yonih | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.