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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    highest, lowest, average

    I am trying to write this program that lets the user put in the temperature at noon everyday for 5 days and then displays the daily temp, highest, lowest, and average. Again I am asking please for someone to look at it and point me in the right direction. Thank You

    JavaScript Page

    Code:
    function tempClick()
    {
    	var tempScore = 0.0;
    	var dailyTemp = 0.0;
    	var highest = -Infinity;
    	var lowest = +Infinity;
    	var sum = 0.0;
    	
    for dailyTemp = 1 to 5 step 1
    	{
    		var dailyTemp = prompt("Enter the noon temp for the day" + index);
    		temp = parseFloat(temp);
    		sum += dailyTemp
    		var averageTemp = sum / tempScore
    	}
    	document.tempForm.dailyTemp.value = dailyTemp;
    	document.tempForm.highestTemp.value = highestTemp;
    	document.tempForm.lowestTemp.value = lowestTemp;
    	document.tempForm.averageTemp.value = averageTemp;
    }
    HTML PAGE

    Code:
    <!doctype html>
    <html>
    	<head>
    		<title>Temp Stats</title>
    		<meta charset="utf-8" />
    		<script type="text/javascript" src="Powers.js"></script>
    	</head>
    	<body>
    		<form name="tempForm" action="#">
    			<label for="day1">Day 1: </label>
    			<input type="text" id="day1" />
    			<br />
    			<label for="day2">Day 2: </label>
    			<input type="text" id="day2" />
    			<br />
    			<label for="day3">Day 3: </label>
    			<input type="text" id="day3" />
    			<br />
    			<label for="day4">Day 4: </label>
    			<input type="text" id="day4" />
    			<br />
    			<label for="day5">Day 5: </label>
    			<input type="text" id="day5" />
    			<br />
    			<label for="highestTemp">Highest Temp: </label><br />
    			<input type="text" id="highestTemp" />
    			<br />
    			<label for="lowestTemp">Lowest Temp: </label><br />
    			<input type="text" id="lowestTemp" />
    			<br />
    			<label for="averageTemp">Average Temp: </label><br />
    			<textarea id="highestTemp" rows="5" cols="3"></textarea>
    			<input type="button" value="Run!" onclick="tempClick()" />
    			<br />
    		</form>
    	</body>
    </html>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,083
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Some major problems.

    1. Invalid 'for' statement.
    2. prompt() is not a very good way to collect user input.
    3. You don't define 'temp' before you use it in the parseInt() function.
    4. Cannot reference element via 'name' if it is defined as 'id'
    5. Why define input as element and then use prompt to collect temps again???

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    for dailyTemp = 1 to 5 step 1
    {
    var dailyTemp = prompt("Enter the noon temp for the day" + index);


    Apart from the several other problems.

    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! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
    for dailyTemp = 1 to 5 step 1
    That's vbscript syntax not javascript.


  •  

    Tags for this Thread

    Posting Permissions

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