Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Need help creating JavaScript function that accepts any array of numbers

    Hi, I'm new to JS programming and I would appreciate any help or advise with helping me solve an issue that I have. I need to create a function that accepts any array of numbers and returns an array of percentage values for each number on a scale of 0 to the highest number in the array. Round any decimals to two decimal places.

    I eventuality would like to call this function from its own JavaScript file and display the return array.

    Array I used to test - [2, 12, 4, 7]

    Output I have thus far:

    12

    0.16666666666666666
    1
    0.3333333333333333
    0.5833333333333334


    My logic to solve this would look something like this:
    1. find the largest number in the array
    2. take each number in the array and divide by the largest number in the array
    3. round each number to 2 decimal places
    4. output to the screen in the format of an array [16.66, 1, 33.33, 58.33]


    This is what I have so far:

    Code:
    <!DOCTYPE html>
    <html>
    	<head>
    
    	</head>
    	<body>
    
    		<p id="number"></p>
    
    <!-- scripts.js -->
    		<script>
    			var myArray = [2, 12, 4, 7];
    			var largestNumber = Math.max.apply(Math, myArray);
    			document.getElementById("number").innerHTML = largestNumber; // display largest number in array
    
    			for (var i=0; i < myArray.length; i++)
    				{
    					myArray[i] /= largestNumber; 			
    					document.write(myArray[i] + "<br />");
    				}
    		</script>
    <!-- scripts.js -->
    
    	</body>
    </html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    document.write(myArray[i].toFixed(2) + "<br />");

    Except, of course, document.write is long obsolete and should be avoided at all costs.

    You are doing the right thing here: document.getElementById("number").innerHTML
    Why would you then blow it when displaying the array?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Why would you then blow it when displaying the array?
    Thanks for all of your help!
    What would you suggest I do to display the array? I'm still trying to learn...

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    innerHTML would work fine.

    Code:
    	var myArray = [2, 12, 4, 7];
    	var largestNumber = Math.max.apply(Math, myArray);
    	document.getElementById("number").innerHTML = largestNumber; // display largest number in array
    
            var result = "";
    	for (var i=0; i < myArray.length; i++)
    	{
    		myArray[i] /= largestNumber; 			
    		result += ( myArray[i].toFixed(2) + "<br />");
    	}
            document.getElementById("result").innerHTML = result;
    And just have <p> or <div> with an id of "result".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    _Megatron (08-09-2014)

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,399
    Thanks
    11
    Thanked 595 Times in 575 Posts
    Code:
    var r=[2, 12, 4, 7];
     document.body.innerHTML=JSON.stringify(
      r.map(
         function(a){return (a/this).toFixed(2) }, 
         Math.max.apply(0, r)
       ), 
     null, "\t" );
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •