5/7 why the function?


#1

Maybe I'm missing something here. But in 5/7:

//Remember to set your condition outside the loop!
var number = 0

var loop = function(){
	while(number < 3){
		console.log("I'm looping!");
		number++;
	}
};

loop();

Why is the while loop inside a function? What is the purpose here?


#2

There are two things here that are not very much explained, the purpose of the function, and the placement of setting the condition. Consider this arrangement...

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

The message is simple... Re-usability. We can run this loop as often as needs be and always get the expected result. Notice that the condition is set inside the function, since that is where the loop resides and the control variable has no purpose outside of the function.


Ex 10. What is the point of the empty function?
Introduction to while loops
#3

We could introduce a limit parameter that would replace the number primitive constant written in the earlier example.

var loop = function (limit) {
    var number = 0;
    while (number++ < limit) {
        console.log("I'm looping!");
    }
};
loop(9);

Output

I'm looping!
I'm looping!
I'm looping!
I'm looping!
I'm looping!
I'm looping!
I'm looping!
I'm looping!
I'm looping!

#4

I think I'm starting to understand. Gonna have to inspect your examples a bit more, but I'm sure I'll get there :smile: Thank you very much!


#5

This should absolutely be included in the instruction. Learners assume these lines are relevant to the task, but they are not, and this is not explained to them.


#6

Thanks much for both those explanations.


#7

Sorry, but still confused. How does having a while loop nested in a function make it more reusable, then just repeating the 'while' code when needed?


#8

WHY is the loop(); at the very end of the code? Doesn't the code run 3 times in this instance and stop? Seems redundant and unnecessary.


#9

A loop is one form of code block that works on repetition/iteration, but it is not re-usable. If we want the same loop again, we have to write it again. This is needless repetition of a program pattern. That's where functions come in... They are a single piece of code that can be called over and over again from anywhere in the program. They can be crafted to accept discrete data, or reference objects, and can return data back to the caller. This is very powerful stuff since it permits us to move data from one scope into another, in a two-way transaction.


#10

loop() is the call expression that invokes the function. The second example above takes a parameter, a number, to indicate the number of iterations in the loop. In that case, we pass an argument to the function:

loop(3);

It is not part of the function, but the means by which the program is able to invoke the function.


#11

This is beauty. :grinning:


#12

Let me see if I understand this correctly.

In a real world setting, loop would exist alongside other code, and would probably be changed at some point. Let's say line 250 is in charge of changing loop. If we use the simple (non-function) version:

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

then changing loop to 95 while keeping the end result of three iterations would be a lot of work You would also have to ensure that the number on line 2 is always three higher. But if the counting variable is nested and kept separate, then line 250 would only need to say loop = 95. Always strive to type as little as possible!

How's that? I think this example is more applicable to a for loop than a while loop but hopefully it still made sense.


#13

mtf you are an amazing teacher. Thank you a lot


#16