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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help counting the number of numbers below the average

    Pretty much what I need to do is input 10 numbers, get the average, and then count how many of the inputted numbers are below the average, but I can't seem to figure out how to count those numbers. How could I get a count of the numbers below the average and then write it to document? Any help is very appreciated!

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title> Lab 12 (Page 105 #2) </title>
    </head>
    
    <body>
    <h1><center>Lab 12 (Page 105 #2)</h1></center>
    
    <hr><hr>
    
    <h4>
    
    <script>
    
    var sum=0;
    var avg=0;
    var inp;
    var count;
    
    for(var x=0;x<=9;++x)
        {
            inp=eval(prompt("Enter Value # "+(x+1)));
            document.write("Number " +(x+1)+ " = "+inp+"<P>");
            sum=sum+inp;
        }
    
    avg = sum/x;
    
    
    document.write("<center>Average = "+avg+"<P>"+"Count Below Average = ");
    
    </script>  
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Get rid of the eval().. eval() is evil. Don't ever use it.

    Don't use document.write.. it's old, outdated, and a less than ideal way of doing this. Set a SPAN tag (before the script tag), give it a unique id, and set the innerHTML of that.

    You will also need to keep an array of each number that is entered, then loop through the array checking to see which values are less than the average, and keep tabs (ie, a=0, for each number below the average a++, display a.)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    This ought to give you a push forward. But if you want the user to enter the numbers in response to a prompt() you need to validate that the value entered is in fact a number.

    Code:
    <script type = "text/javascript">
    
    var nums = [2,3,4,5,6,7,99];
    var len = nums.length;
    var tot = 0;
    var belowav = 0;
    
    for (var i =0; i<len; i++) {
    tot += nums[i];
    }
    var average = tot/len;
    
    for (var i=0; i<len; i++) {
    if (nums[i] < average) {
    belowav ++;
    }
    }
    
    alert ("The mean average of the " + len + " numbers is " +  average + ".  \nOf these " + belowav + " are below the average value.")
    
    </script>
    To prompt repeatedly x times until the user has entered x numbers:-

    Code:
    <script type = "text/javascript">
    
    var howmany = 10;
    for (var j = 0; j<(howmany-1); j++) {  // prompt for howmany numbers.  You will need to store them in an array.
    for (var i=1; i<2; i++) {
    var ans = parseFloat(prompt ("Enter a number", ""));
    if ((isNaN(ans)) || (ans == "")) {
    alert ("You must enter a number!!  ");
    i -- ;
    }
    }
    }
    
    </script>
    Both document.write() and alert() are considered to be obsolete and suitable only for demonstration (as here) or debugging purposes.
    prompt() is also a very primitive way of obtaining user-entered data.


    "Copy from one book, it’s called plagiarism; copy from three, it’s called research." -- Wilson Mizner (1876-1933)
    Last edited by Philip M; 12-11-2012 at 08:54 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Yeah, guys, but look here:
    <title> Lab 12 (Page 105 #2) </title>

    Another person who is being taught the History of JavaScript (as Felgall would say) instead of correctly being taught Modern JavaScript Programming
    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.

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    That's not only history of JavaScript but history of HTML as well since many of those HTML tags became obsolete in 1997.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    haha ill look into it tomorrow, my teachers old and outdated and teaching us all the old ways. Everything in this document was mostly teaching from him

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,016
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by jeff-tranchina View Post
    haha ill look into it tomorrow, my teachers old and outdated and teaching us all the old ways. Everything in this document was mostly teaching from him
    As I have said before, is there any other subject - computer hardware, chemistry, law, or medicine, where tutors are allowed to get away with teaching old and long-obsolete material?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I agree haha


  •  

    Posting Permissions

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