Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2012, 06:19 PM   PM User | #1
jeff-tranchina
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jeff-tranchina is an unknown quantity at this point
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>
jeff-tranchina is offline   Reply With Quote
Old 12-11-2012, 07:28 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 946
Thanks: 7
Thanked 97 Times in 97 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 12-11-2012, 08:48 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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)
__________________

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.

Last edited by Philip M; 12-11-2012 at 08:54 PM..
Philip M is offline   Reply With Quote
Old 12-11-2012, 11:39 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-12-2012, 01:46 AM   PM User | #5
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,452
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-12-2012, 03:05 AM   PM User | #6
jeff-tranchina
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jeff-tranchina is an unknown quantity at this point
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
jeff-tranchina is offline   Reply With Quote
Old 12-12-2012, 07:29 AM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-12-2012, 02:27 PM   PM User | #8
jeff-tranchina
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jeff-tranchina is an unknown quantity at this point
I agree haha
jeff-tranchina is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:12 AM.


Advertisement
Log in to turn off these ads.