Im trying to practice loops in Javascript. I have a program that asks for the temperatures for the last week then works out its average and prints a text depending on the results. I have added into it to return an error if the number entered is not between -30 and 40 and stop the loop. This works ok apart from I still get the other text based on the average. I understand that using break like I am will still continue the rest of the code after the loop which is why Im getting the other text still. I am just starting out in the basics so it should be as simple as possible. How can I do it so as not to get the other text, what should I be using instead?
<meta charset="UTF-8">
<title>Lämpötilat</title>
<script language="JavaScript">
var lampotila=0;
var i;
for (var i = 0; i < 7; i++) {
lampotila = Number(prompt("Enter the temperatures for the last 7 days"));
if(lampotila <-31 || lampotila > 40) {
document.write ("ERROR: You must enter a temperature between -30° and 40°");
{break;}
}
}
yht = yht + lampotila;
k = yht / 7
ki = k.toFixed(2);
if (ki < -10) {
document.write ("Kylmää talvisäätä!");// cold winter weather
}
else if (ki <0 && ki> -10) {
document.write ("Hyvä hiihtosää!");// good skiing weather
}
else if (ki >0 && ki <10) {
document.write ("Kevät tulossa!");// spring is coming
}
else {
document.write("T-paita vois riittää!");// t- shirt should be enough
}
</script>
document.writewithalert.console.logpreferred overalert- however, in this simple case,document.writeis the better choice over both alert and console (learning to manipulate the DOM would be ideal)