Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

intro computer science hw (Chapter #6 Question) You must use a LOOP in this Ques

ID: 3591072 • Letter: I

Question


intro computer science hw

(Chapter #6 Question) You must use a LOOP in this Question (You DO NOT need an ARRAY as arrays haven not been covered yet!!!) 6, Develop an algorithm and create a Weekly Weather Forecast Web Page containing the JavaScript code that allows a user to enter a temperature in degrees Celsius or degrees Fahrenheit (your choice) for seven days of a week. Use a prompt box for the input, use only ONE variable for the daily temperature (NOT AN ARRAY), and use a loop to obtain all 7 temperatures from the user. Additionally use a variable to hold the lowest and the highest temperature for the week (the logic for this is to initialize each of these 2 variables (low and high) with the first temperature entered by the user do not set them to 0 as temperatures can also be negative numbers). Your code will display the AVERAGE temperature, the HIGHEST temperature, and the LOWEST temperature for the week in the web page (with 2 decimal places SEE PREVIOUS ASSIGNMENTS FOR HOW TO DISPLAY 2 DECIMAL PLACES USING THE toFixed(2) FUNCTION). Also display the appropriate message that describes the weather for that week using the scale in step 5 above and your calculated AVERAGE weekly temperature. CHANGE THE MESSAGES IN STEP 5, associated with the scale, TO YOUR OWN ORIGINAL MESSAGES. Save this file as weeklyForcast.html in the folder created in step #4 above (YourName_MFC132 Assign3). Test it in a browser to make sure it works.

Explanation / Answer

<html>
<body>

<form action = "showForecast()">
<!--Input text box-->
Enter Temperature of this week in Celcius:<br>
<!-- t1,t2,t3,t4,t5,t6,t7 representing teperature of the week startign from sunday to saturday -->
Sunday : <input type = "number" name = "t1" placeholder="Sunday Temperature" /><br>
Monday : <input type = "number" name = "t2" placeholder="Monday Temperature" /><br>
Tuesday : <input type = "number" name = "t3" placeholder="Tuesday Temperature" /><br>
Wednesday : <input type = "number" name = "t4" placeholder="Wednesday Temperature" /><br>
Thursday : <input type = "number" name = "t5" placeholder="Thursday Temperature" /><br>
Friday : <input type = "number" name = "t6" placeholder="Friday Temperature" /><br>
Saturday : <input type = "number" name = "t7" placeholder="Saturday Temperature" /><br>
<input type = "submit" />
</form>

<div id="avgTemp"></div>
<div id="maxTemp"></div>
<div id="minTemp"></div>

<div id="forecast"></div>

<script type="text/javascript">
function showForecast(){
var avg_temp;//average temperature
var max_temp;//Highest temperature
var min_temp;//Lowest temperature

//get the input variables from the input box

var t1 = document.getElementsByName("t1")[0].value;
var t2 = document.getElementsByName("t2")[0].value;
var t3 = document.getElementsByName("t3")[0].value;
var t4 = document.getElementsByName("t4")[0].value;
var t5 = document.getElementsByName("t5")[0].value;
var t6 = document.getElementsByName("t6")[0].value;
var t7 = document.getElementsByName("t7")[0].value;

// initialize highest and lowest temperature
max_temp = t1;
min_temp = t1;

//loop over the weeks temperature
for(var i=1;i<8;i++){
if(eval('t'+i) < min_temp){
min_temp = eval('t'+i);
}

else if(eval('t'+i) > max_temp){
max_temp = eval('t'+i);
}
}

//find the average temperature

avg_temp = (t1+t2+t2+t4+t5+t6+t7)/7;

//print teh average , highest and lowest temperatur of the week

//using innerHTML to show the content fro Javascriptt to html
document.getElementById('avgTemp').innerHTML = "Average Temprature of the week : "+avg_temp+"C";
document.getElementById('maxTemp').innerHTML = "Highest Temprature of the week : "+max_temp+"C";
document.getElementById('minTemp').innerHTML = "Lowest Temprature of the week : "+min_temp+"C";

//since the messages and the criteria is not given in the question, i am just typing a random message.
//Please input your own message in here
//Also, you can change the classification

if(avg_temp > 35){
document.getElementById('forecast').innerHTML = "Its a sunny day";
}
else if(avg_temp<25){  
document.getElementById('forecast').innerHTML = "Its a cold day";
}
else{
document.getElementById('forecast').innerHTML = "Its a normal day";
}

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