I am supposed to make this echo “hello” twice while only changing two things: fix the iterator so it increments each time, and set the while loop so it only runs twice. I’m a beginner so I’m not sure how to do this, really. Any suggestions?
var times = 0;
while ( ) {
console.log( "hello" );
times
};
whileis a loop that takes an expression. While the expression is truthy (true for all intents and purposes), it will execute the code in the following statement or block.So, how would you keep a counter for a loop? You want the counter to start at zero and go up to 2, then, once it reaches 2, it should stop. Suffice to say, you want to loop while your counter is less than 2, that is,
< 2. So your expression is:and your loop is therefore:
Now, you’ll also need to increment your
timesvariable so it goes up each time around the loop. There are a couple ways to do this. A nice and clear one should be:But people usually contract it to
times += 1;, or for incrementing by one:So, your loop should end up as: