Why is function necessary in last while or do/while loop



I'm not getting any error messages, I'm just curious.

If I run the last while or do while loop in an exercise with more than one iteration using a variable as a counter (in this case "i") and print to console it prints the value of that count variable after the expected lines just once at the end. I was getting a value of 2 (counting 3 iterations up from 0) at the end and tested by setting the value to 0 after the loops, and then it prints the 0. It prints all my text properly to console and gives me a green indicator of Success either way and only prints the variable value once after the last loop. I find I only have to encapsulate the last while loop in an exercise to correct this, but am not sure why it's doing this. My code shows the loop function which stops it from printing the "0" after the text.

// Write your code below!
var i = 0;
for(i = 0;i < 3;i++){
    console.log("iterate the for loop 3 times");
i = 0;

    console.log("iterate the while loop 3 times");

 var loop = function(){   
i = 0;
    console.log("iterate the do/while loop 3 times");



It is the Javascript interpreter which is running in this course-environment,
it display's the last non-displayed action...
Which in this case is your i=0;

You will also encounter this, when you are doing a return-statement
as last action...
You could counter this by using a
console.log( " " ); as last statement....

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.