Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Need help counting the number of numbers below the average

1. ## 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>
<title> Lab 12 (Page 105 #2) </title>

<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>```

• 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.)

• 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)

• 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

• That's not only history of JavaScript but history of HTML as well since many of those HTML tags became obsolete in 1997.

• 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

• Originally Posted by jeff-tranchina
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?

• 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
•