0

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>
8
  • 1
    For starters, try replacing document.write with alert. Commented Dec 1, 2016 at 12:41
  • 3
    @georg - console.log preferred over alert - however, in this simple case, document.write is the better choice over both alert and console (learning to manipulate the DOM would be ideal) Commented Dec 1, 2016 at 12:42
  • I go with alerts over logs since many browsers I work with don't have console objects defined by default. Commented Dec 1, 2016 at 12:44
  • Its working how I want it almost apart from I just want it to return the Error message if the parameters are not met. Ideally I would like it to go give the error then start off from where it ended again but I have even less clue how to do that :D I did change it to alert however. Commented Dec 1, 2016 at 12:48
  • @takendarkk which browsers don't have a console? Commented Dec 1, 2016 at 12:50

3 Answers 3

1

Increasing the indentation may help making the code more readable and easier to detect errors and bugs.

From http://www.w3schools.com/js/js_break.asp we know that break; lets us get out of a loop. So, after the break no more iterations will be made inside that loop and the computer will continue executing what is after that loop.

Your second part of the code is outside the loop, so doing break in the loop will not affect the rest. You could have a variable that indicates if an error was found while inputting data. If so, then don't execute the rest of the code.

Something like this:

var lampotila=0; 
// var i; <= You don't need this if you `var i=0` inside the foor loop. 
// Also, you don't use i outside the loop so there is no need 
// to have it declared in the global scope.
var error = false; // <== THIS LINE NEW

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°");
    error = true; // <== THIS LINE NEW
    break;
  }
}

if (!error) { // <== THIS LINE NEW
    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
    }
} // <== THIS LINE NEW
Sign up to request clarification or add additional context in comments.

Comments

0

You can decrease i value before break. So, user must enter true temperature value.

if(lampotila <-31 || lampotila > 40) {
document.write ("ERROR: You must enter a temperature between -30° and 40°");
i = i - 1;
continue;
}}

2 Comments

Thank you so much! It seems so simple now I look at it. Please tell me it gets easier :D
To get it to minus the error number I put yht=yht-lampotila also and now it seems to work ok.
0

You have a syntax error in the below line:

{break;}

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.