...

View Full Version : Java Script Beginner. For Loop Question.



HeadSetJones
11-18-2012, 03:43 AM
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



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

felgall
11-18-2012, 04:40 AM
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.

HeadSetJones
11-18-2012, 04:53 AM
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.

felgall
11-18-2012, 06:02 AM
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):

http://javascriptexample.net/inc/prompt.gif

HeadSetJones
11-18-2012, 08:51 PM
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-Program-Design-Problem-Solving-JavaScript/dp/1111526508/ref=sr_1_1?ie=UTF8&qid=1353271527&sr=8-1&keywords=problem+solving+with+javascript

Philip M
11-19-2012, 07:55 AM
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].


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

Philip M
11-19-2012, 10:59 AM
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!

firepages
11-19-2012, 04:00 PM
closing thread since it was getting hijacked, HeadSetJones please start another thread if you need further help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum