473,598 Members | 2,978 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_2008 J_Q1_task01.htm l replacing the existing
JavaScript with the function,
getRoundedRando mNumber(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 getRoundedRando mNumber() function into your Solution
Also remove the line of code and associated comment you wrote
for part (ii).
Read the ONCLICK event handler for the file’s HTML form to
understand how getRoundedRando mNumber() 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>
  5. <SCRIPT language = "JavaScript">
  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
  11. var getRoundedRandomNumber = Math.round(Math.random()*49);
  13. window.alert("Random number returned is: " + getRoundedRandomNumber);
  16. //generate a random number and round down
  18. var getRandomNumber = Math.floor(Math.random()*49)
  20. </SCRIPT>
  21. </HEAD>
  22. <BODY>
  23. <STRONG>A test of the random number functions <BR></STRONG>
  24.     <FORM NAME = "randomForm">
  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 5594
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" … >
  3. <script type="text/javascript>
May 7 '09 #2
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
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
  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
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);
  5. return aNumber;
  6. }
Help please lol
May 7 '09 #5
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
16 New Member
I'm typing on my iPhone in work and using notepad to right the script lol
May 7 '09 #7
16,027 Recognized Expert Moderator MVP
But you must be testing it in a browser, no?
May 7 '09 #8
16 New Member
Ah i see what you mean it says 'aNumber' is undefined
May 7 '09 #9
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

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 Children aged 4 to 12 4) Number of Children under age 4
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. Using the javascript debugger, it seems that everything is working at that end, so I wonder if the problem is mozilla/css related. The live page is at http://www.smalltime.com/exhibitions/washanddry Here is the relevant html and javascript...
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 Mozillas, neither Firebird nor Firefox (and not in Netscape 7.something). What is wrong with it? I'm no javascript expert, I just got it from the net years ago, I don't even know where from. There is no source url given in it to look up. I've...
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? Here's my code (CMS and INCHES which is working): <input name="CM" type="text" VALUE="" size="3" maxlength="3" onChange="eval('IN.value = ' + this.form.IN_expr.value)"> cms <em>or</em> <input name="IN" type="text" VALUE="" size="3" maxlength="3"
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 could help me out here. Below is the complete script. <SCRIPT language="JavaScript1.2"> var cursorpath="http://i68.photobucket.com/albums/i9/worklog_halcyon/Misc/pet1_lohi_dog.png" if (document.layers)
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! The 'floating layer' does not float in IE7... it just sits up at the top of the screen - stuck in one place!! Explorer - why do you mock us?!!! ;( I have tried everything - but I am at a loss. I am thinking maybe it's some conflicting CSS or some...
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 the "Downpayment" and also the "Total amount paid" Example: item cost $20.00 , $8.50 shipped selected, Payment 1 = 12 + 8.50 so $20.50 Payments 2-5 = $2.00 Final Payment = $28.50
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> <SCRIPT language=JavaScript>
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.