0

Hello everyone,
I'm learning JavaScript programming and I have a test to do. Will it be possible for someone to check what I've already written and let me know if I'm on the right track?
In green are the questions asked and in red are my answers. I also put the original code in black.
Thank you in advance for your time and guidance.

Open M150_TMA05_2009B_Q1_task1.html in the text editor and read through the code to see how Math.random() is used. Run the program, observing the output in the alert dialogue. When you close the alert dialogue box a ‘Display Random Number’ button is visible in the browser window: ignore this for now.

<HTML>
<HEAD>
<TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>

<SCRIPT language = "JavaScript">
	
		//generate a random number greater than or equal to 0.0 and less than 1.0 
		//and display it in an alert dialogue
		window.alert("Random number returned is: " + Math.random());
	
</SCRIPT>
</HEAD>
<BODY>
<STRONG>A test of the random number functions <BR></STRONG>
	<FORM NAME = "randomForm">
	
	<INPUT TYPE = "button" NAME = "randomButton"  VALUE ="Display Random Number"
			ONCLICK = "window.alert(getRoundedRandomNumber(50));">
	</FORM>


</BODY>
</HTML>

Edit M150_TMA05_2009B_Q1_task1.html so that the alert dialogue box will display numbers greater than or equal to 0.0 and less than 50.0. Remember to make appropriate changes to the code comment.

<HTML>
<HEAD>
<TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>

<SCRIPT language = "JavaScript">
	
		//generate a random number greater than or equal to 0.0 and less than 50.00 
		//and display it in an alert dialogue

		window.alert("Random number returned is: " + Math.random()*50);
	
</SCRIPT>
</HEAD>
<BODY>
<STRONG>A test of the random number functions <BR></STRONG>
	<FORM NAME = "randomForm">
	
	<INPUT TYPE = "button" NAME = "randomButton"  VALUE ="Display Random Number"
			ONCLICK = "window.alert(getRoundedRandomNumber(50));">
	</FORM>


</BODY>
</HTML>

Edit M150_TMA05_2009B_Q1_task1.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.

<HTML>
<HEAD>
<TITLE>M150 TMA 5 : Programming : Task 1 - Testing Math.random()</TITLE>

<SCRIPT language = "JavaScript">
	
		/*Generate a random number greater than equal to 0.0 and less than aNumber.
		Create a function to round the random number generated and display it using the event handler Onclick.*/
		
		var aNumber = parseFloat((window.alert("hello" + Math.random()*50))) ;
		
	
		//create the function getRoundedRandomNumber( ) to be called when Onclick is pressed
		
		function getRoundedRandomNumber(aNumber)
		// round the number generated by the random
		
		{ 	var aRoundedNumber = Math.round(Math.random() * (aNumber)) ;
		// show the result of the number
			return aRoundedNumber ;
		} 
		
	
</SCRIPT>
</HEAD>
<BODY>
<STRONG>A test of the random number functions <BR></STRONG>
	<FORM NAME = "randomForm">
	
	<INPUT TYPE = "button" NAME = "randomButton"  VALUE ="Display Random Number"
			ONCLICK = "window.alert(getRoundedRandomNumber(50));">
	</FORM>


</BODY>
</HTML>

Edited by Ezzaral: Added [code] [/code] tags. Please use them to format any code that you post.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by JugglerDrummer
0
var aNumber = parseFloat((window.prompt("hello, what's the max number?","")) ;
function getRoundedRandomNumber() //no arg, use already existing var

// round the number generated by the random

{ var aRoundedNumber = Math.round(Math.random() * (aNumber)) ;

// show the result of the number

return aRoundedNumber ;
}
ONCLICK = "window.alert(getRoundedRandomNumber());">
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.