0

var temp = 110;
for {
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }
    
}while (temp > 90)

Please review my snippet. It won't run for some reason stating some bracket error as I already checked the brackets.

4
  • 2
    You're using for wrongly. Replace it to do. Commented Jun 26, 2015 at 4:24
  • 2
    Replace for with do Commented Jun 26, 2015 at 4:25
  • I have a question for you! If you are asking the user for a prompt, you shouldn't be using a while loop because, that will make your loop go on forever. Commented Jun 26, 2015 at 4:38
  • I see. Sorry for my ignorance. I just want to see how to implement it...sooner or later i will realize by myself the letting user prompt the code was a mistake. =\ thank you for your advise! Commented Jun 26, 2015 at 4:40

4 Answers 4

2

Its do not for

var temp = 110;
do { //its `do` not `for`
  temp -= 1;
  if (temp >= 90) {
    console.log("Today's temperature is " + temp + "! " + "Lets go play ball")
  } else {
    console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!")
  }

} while (temp > 90);

You may use prompt() for user input:

var temp = prompt('Input temperature', '110'); // (message, default input)
console.log('temp', temp);
do { //its `do` not `for`
  temp -= 1;
  if (temp >= 90) {
    console.log("Today's temperature is " + temp + "! " + "Lets go play ball")
  } else {
    console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!")
  }

} while (temp > 90);

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! now is there a way to only display one temp Prompt(ing) the user temperature?
thank you for your hastily update! Is there a way to only control.log the prompt temp?
1

The do while loop syntax is like this:

Example

do {

}while(conditions);

So a do-while loop with a nested if/else statement is like this:

do-while w/ nested if/else

do{

if() {
}
else {
}
}while();

Example w/ YOUR CODE

var temp = 110;

do {
 if(temp >= 90) 
{
    console.log("Today's temperature is "+temp+"! "+"Lets go play ball");
{
else 
{
    console.log("Today's temperature is "+temp+"! "+"It is too hot today to play      ball!");
}
 temp--; 
} while(temp > 90);

Okay now, let me explain what is happening here. What you are essentially doing is telling the compiler to do something until the while loop returns true. So, if you notice I changed temp -= 1; to temp--; it is exactly the samething it's just much more standard to use the latter. You were actually very close with your original code other than it is a do-while loop not a for-while. :)

6 Comments

thank you so much for your detailed explanation! if there a way to revise the code that only display the console.log of the specified temp the user put in?
Yeah just output the users input. var sample = prompt("Input Temp"); Then print out the users input like this, console.log(sample);
Make sure to mark this as the answer, it helps me out and others! :)
What exactly are you trying to do? That way I can write out a code that is specific to what you are trying to do.
I wanted to console.log one or the other of the if statement. however, it displaying all the iterations of console.log. then I realize it is suppose to show since it did in a loop function. Problem solved thank you for your time!
|
1

Replace for with do. like this

var temp = 110;
do{
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }

}while (temp > 90)

1 Comment

Thank you! now is there a way to only display one temp Prompt(ing) the user temperature?
1

It should be

var temp = 110;
do {
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }
    
}while (temp > 90);

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.