intro computer science hw (Chapter #6 Question) You must use a LOOP in this Ques
ID: 3591072 • Letter: I
Question
intro computer science hw
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>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.