Why do need a "function" with the while loop?


#1

here's my code with no function:

var count = 0;
while(count < 3){
console.log("I'm looping!");
count += 1;
};

It's NOT perfect because it prints the integer 3 before code terminates!
is that why the function?


#2

@sadmaze,

++++ why second display of last Item
+++++++++++++++++++++++++++++
It is the Javascript interpreter which is running in this course-environment,
it display's the last non-displayed action...

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 to EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );


#3

@sadmaze,

They try to demonstrate,
that you can access AND modify a so-called =global= variable count
from within a function with it's FUNCTION-BODY

closure scope hoisting
https://www.codecademy.com/forum_questions/54936d24e39efeeb1d000111#comment-5499a6ea9113cbdac9009bb0
https://www.codecademy.com/forum_questions/55d0b0c7937676b904000522


#4

Thank you Leonard!
I am understanding better, the more I practice.
thanks again for your help.