1

Exercise:

Declare a variable named countdown and assign it the value of 10. In a while loop, decrement the value of countdown once for every iteration and print it. Once countdown hits 0, print 'Blastoff' to the console.

What I've done so far:

var countdown = 10;{
    while (countdown > 0 || 0=== "Blastoff!"){
        console.log(countdown);
        countdown = countdown - 1;
    }
    console.log("Blastoff!");
}

This is the result until now that i thought it should be correct:

Output
>>>>Code is incorrect
The first line in your while's block should decrement the value of the variable countdown
10
9
8
7
6
5
4
3
2
1
Blastoff!
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

3 Answers3

1

You are almost there but have a few small mistakes to fix. First, remove this open bracket after var countdown = 10;{ (I get it that it might be added by mistake when writing the question here).

Second, remove this part || 0=== "Blastoff!" from the while conditions. This will always be false.

Third, and probably most important, move the decrement part countdown = countdown - 1; before the console.log.

Here's an example how your code will look after the changes:

var countdown = 10;
while (countdown > 0){
    countdown--;
    console.log(countdown);
}
console.log("Blastoff!");
George Pandurov
  • 384
  • 3
  • 15
  • 1
    An explanation would be in order. E.g., what is the idea/gist? What did you change? What was wrong with the original code? What was the crucial mistake in the original code? What kind of error? A logical mistake? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/65205615/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Jan 19 '22 at 17:21
1

It is finally correct:

var countdown = 10;
while (countdown > 0){
    countdown--;
    console.log(countdown);
}
console.log("Blastoff");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1

Try this:

var countdown = 10

// Changed condition to greater than 1
while (countdown > 1){

    // Printing decremented countdown
    console.log(--countdown)
}

console.log("Blastoff!");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131