While loops coincide with for loops



Hi Everyone!
<Below this line, add a link to the EXACT exercise that you are stuck at.>
This topic is a simple, but general coding discrepancy within my brain. Well, as you can see, the code below is what happened when there was an open coding space. I just went nuts. Please mind the grammar and spelling mistakes in the code. This topic is going to center mostly around the for and while loop. My situation is when I run this code, the console logs all of the statements in ‘for’ but not for ‘while’. The behavior I wanted the code to have was log the statement “filler” in the console as the number counted up, as many times the input started. This part worked, but the while loop was ignored. I wanted the while loop to log “what” when the input, in the for loop, reached 8. I did not log the statement. why is this?

I do understand that for loops repeat the actions in the brackets, as long as the condition stays true. While loops are the same, as they do whatever is in the brackets, stopping at the condition, but they’re a bit broad. While loops allow for not only number conditions but Boolean conditions, so you can set all sorts of things. Also, while loops run because a programmer might not know how often the code will run. Now that you know that I understand what these two loops are, why do the statements in the while loop not log as long as the for loops is running and logging statements into the console?

<In what way does your code behave incorrectly? Include ALL error messages.>


var input = prompt(“Waht meat do ya like?!”);
var input2 = prompt(“Pick a number from 1 through 5”);

for(var input2 = input2; input2 < 10; input2++)

while(input2 < 8)
case “meat”:
case “bacon”:
console.log(“Glory to Mother Bacon”);
case “vegestables”:
console.log(“no, just no”);
console.log(“didn’t get that”);

<do not remove the three backticks above>


I am glad you are yourself trying to figure out how code works :slight_smile:

Before jumping to your code’s problem, let me clear one thing:

This is wrong. Remember that “All for loops can be converted to a while loop, and all while loops can be converted a for loop”. for loops can also allow boolean conditions, like this:

// loops only once, but you get the point
for(var boolean = false; boolean !== true; boolean = !boolean)
// also, instead of `boolean !== true`, we can simply write `boolean`

Now, coming to your code’s problem, the problem is easy to spot:

var input2 = prompt("Pick a number from 1 through 5");

for(var input2 = input2; input2 < 10; input2++)

while(input2 < 8)

There is only one input2 variable[1]. By the time the for loop finishes execution, the value of input2 is such that the for-loop’s condition (input2 < 10) has become false (remember that a loop only ends when its test condition evaluates to false).

What value of input2 will make that condition false? And how does that relate to while loop’s test condition input2 < 8?

I will leave it to you to explore :wink:

[1]: You might be wondering that you have done this:

var input2 = prompt("Pick a number from 1 through 5"); // line A

for(var input2 = input2; input2 < 10; input2++) // line B

so there should be two input2 variables, one for the loop and the other input by the user. But, NO, that is not the case. var keyword creates a variable inside the scope of its closest parent function (and in the global scope, in case there’s no function). Line A creates a input2 variable in the global scope. Line B’s this portion:

var input2 = input2

does nothing but reassign input2 its own value back to itself. So, you still just have one input2 variable.


Thank you for the wonderful reply! I’ll keep going with this problem and solve it now that I received great help! Thank you!