473,769 Members | 7,097 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
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 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>
  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 5617
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
3322
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
2
9504
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...
8
1500
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...
8
2529
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"
1
1785
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)
2
5410
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...
4
1792
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
1
1220
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> <SCRIPT language=JavaScript>
0
9589
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, 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...
0
9423
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,...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, 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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
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
2
3563
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.