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

Before you post, read our: Rules & Posting Guidelines

Closed Thread
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-18-2012, 03:43 AM   PM User | #1
HeadSetJones
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
HeadSetJones is an unknown quantity at this point
Smile Java Script Beginner. For Loop Question.

Hello, Been meaning to join this forum for a while but just now got around to it due to a problem I'm having. I was given an assignment to design a program that accepts five daily temperature readings and display the highest, lowest, and average temperatures. I'm supposed to use a For loop to take the five readings. I'm having trouble in figuring out how to use the data entered by the user to preform calculations on them. As in getting them from the user and storing them to use. So far this is the code I've gotten

Code:
<html>
<body>
<script type="text/javascript">

// Program name: tempStats
// Purpose: TempStats, five daily temps. 
// display high, low, and mean using "forloop"
// Author: Bran
// Date last mod: 11-12-'12 


// Declare Var and Con

var ES = "";
var BR = "<br/>";
var index;
var PA = "<PA/>";
var temp;
var numTemp;


//Welcome
document.write("Welcome to Temperature Stats" + BR);

// Asking for Input from user
document.write("Enter the number of Temperatures to analyze" + BR);
numTemps = prompt ("Enter the number of temperatures to analyze:", ES);
numTemps = parseFloat (numTemps)


// forloop

for (index = 1; index <= numTemps; index++) {
	temp = prompt
("Enter the degrees for temperature # " + index,ES);
temp =parseFloat
}

</script>
</body>
</html>
I was thinking I was going to use max() and min() and for the mean just use sum and then / by 5 but I can't use the numbers entered by the user in any way. I'm sure this is a very easy problem but I've been stuck on it for a while. Maybe I'm just playing it to close to the vest but if anyone could help, I'd be very thankful.
HeadSetJones is offline  
Old 11-18-2012, 04:40 AM   PM User | #2
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
1. Don't use prompt() as it is intended for debugging only - use a form in your web page instead to collect the values.

2. Don't use document.write - that went out of use when Netscape 4 died as subsequent browsers support the far more powerful innerHTML call instead.

If someone is actually teaching either of those then they need to take a beginners JavaScript class to learn how to write JavaScript for 21st century web browsers.

To be able to advise how to set up a for loop to process the five input fields we will first need to see how you have defined the form. You can convert strings to numbers using either Number(str) or (+str)

You can use Math.max() and Math.min() to get the maximum and minimum values.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline  
Old 11-18-2012, 04:53 AM   PM User | #3
HeadSetJones
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
HeadSetJones is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post

If someone is actually teaching either of those then they need to take a beginners JavaScript class to learn how to write JavaScript for 21st century web browsers.
I'm taking a class using a book published February 22, 2011 as the material and this is week 13 of the class... Haha. Maybe my college and instructor are both behind times.
HeadSetJones is offline  
Old 11-18-2012, 06:02 AM   PM User | #4
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
Week 13 of a JavaScript course and you are only up to using for loops? Or does the course cover a number of web languages and you are just onto the small section of the course that attempts to cover JavaScript?

Which book is it? There have been one or two JavaScript books that I know of that were published after 2006 that still taught antiquated code like that but none that I know of that were published as recently as 2011.

Here's how a prompt looks in some browsers now - note the checkbox for turning off JavaScript (some other browsers have the checkbox simply stop any further prompts from appearing instead so that the following code would run but using an empty string for the remaining inputs):

__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline  
Old 11-18-2012, 08:51 PM   PM User | #5
HeadSetJones
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
HeadSetJones is an unknown quantity at this point
The class is called "Program Design and Development". We only learn Java Script in it. Oh, that and some QBasic. Haha. Week 13 and yes, just now getting into For Loops.

This is the book we're using
http://www.amazon.com/Principles-Pro...ith+javascript
HeadSetJones is offline  
Old 11-19-2012, 07:55 AM   PM User | #6
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 will move you forward. I leave you to work out how to find the highest and lowest values in the array. Hint - set initial value of highest to -99999 and then loop through the array, if data[i] is greater than highest then highest = data[i].

Code:
<html>
<body>
<script type="text/javascript">

// Program name: tempStats
// Purpose: TempStats, five daily temps. 
// display high, low, and mean using "forloop"
// Author: Bran
// Date last mod: 11-12-'12 


// Declare Var and Con

var ES = "";
var BR = "<br/>";
var index;
var PA = "<PA/>";
var temp;
var numTemp;


//Welcome
document.write("Welcome to Temperature Stats" + BR);  // you can only use document.write() on page load.

// Asking for Input from user
numTemps = prompt ("Enter the number of temperatures to analyze:", ES);
numTemps = Number(numTemps) || 0;  // trap NaN entries  Use Number() rather than parseFloat()

// for loop
var data = [];   // an array to hold the temperatures
for (index = 0; index < numTemps; index++) {  // note array index starts at 0
temp = prompt ("Enter the degrees for temperature # " + (index+1),ES);
temp = Number(temp) || 0;
data[index] = temp;
}

alert (data);  // for testing
var total = 0;
for (var i = 0; i<data.length; i++) {
total += data[i];
}
var average = total/data.length;
alert (average);  // when displaying values you will want to use .toFixed(2)


</script>
</body>
</html>

<body>

</html>

The book you mention is advertised at $87.32 . Surely that is a joke!
__________________

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; 11-19-2012 at 08:08 AM..
Philip M is offline  
Old 11-19-2012, 10:59 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 minder View Post
That's a poor way to do it in general. The same logic for an initial value might come back to bite you in another situation. A better way is to set the initial highest value to the first data value and then check each subsequent data value to see if it is higher or lower to get the max. value of the dataset. When you get a higher value, set the highest value variable to the new highest value.
Rubbish, bullant! No temperture can be as low as -99999. In other situations I would use another (superior) method to get the min and max values of an array, not the beginner's method I suggest. You can only hope to improve!
__________________

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  
Old 11-19-2012, 04:00 PM   PM User | #8
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,890
Thanks: 5
Thanked 79 Times in 78 Posts
firepages will become famous soon enough
closing thread since it was getting hijacked, HeadSetJones please start another thread if you need further help.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline  
Closed Thread

Bookmarks

Tags
java forloop loop code

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:47 AM.


Advertisement
Log in to turn off these ads.