Help with JavaScript math.round etc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaiOz
    New Member
    • May 2009
    • 16

    Help with JavaScript math.round etc

    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.

    Code:
    <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.0 
    		//rounded to a whole number
    		//and display it in an alert dialogue
    		
    var getRoundedRandomNumber = Math.round(Math.random()*49);
    
    window.alert("Random number returned is: " + getRoundedRandomNumber);
    
    
    //generate a random number and round down
    
    var getRandomNumber = Math.floor(Math.random()*49)
    
    </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(getRandomNumber);">
    	</FORM>
    </BODY>
    </HTML>
    Last edited by Dormilich; May 7 '09, 04:15 AM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.
    Code:
    <form action="[I]name_of_page[/I]" … >
    
    <script type="text/javascript>

    Comment

    • DaiOz
      New Member
      • May 2009
      • 16

      #3
      Hi, any chance of an example of what function code you would use, I'm pretty useless but trying to learn lol

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        the task requires you to write
        Code:
        function getRoundedRandomNumber(aNumber)
        {
          // put your code here
        
          return roundedNumber;
        }
        and then write some code to call this function onclick.

        hint: the basic idea is outlined in the task

        Comment

        • DaiOz
          New Member
          • May 2009
          • 16

          #5
          My attempt was as follows;

          Code:
          Function getroundedrandomnumber(aNumber)
          {
          var aNumber = Math.floor(Math.random()*49);
          
          return aNumber;
          }
          Help please lol
          Last edited by Dormilich; May 8 '09, 05:45 AM. Reason: added [code] tags

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            JavaScript is case-sensitive, so make sure the case is absolutely correct and also look at the error console.

            Comment

            • DaiOz
              New Member
              • May 2009
              • 16

              #7
              I'm typing on my iPhone in work and using notepad to right the script lol

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                But you must be testing it in a browser, no?

                Comment

                • DaiOz
                  New Member
                  • May 2009
                  • 16

                  #9
                  Ah i see what you mean it says 'aNumber' is undefined

                  Comment

                  • DaiOz
                    New Member
                    • May 2009
                    • 16

                    #10
                    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

                    Comment

                    Working...