How can "function" work with "for" (loops)?


#1

Hi, can you provide me a specific example (programming) about how "function" works with "for" (loops)?


#2

If you wanted a function to print something 3 times:

var printThree = function(str) {
    for (i = 0; i < 3; i++) {
        console.log(str);
    }
}
printThree("WAFFLES!");

Output:

WAFFLES!
WAFFLES!
WAFFLES!

#3

So what does "function" mean here? Why can't I just program my work like this below?
var printThree=["WAFFLES!", "WAFFLES!", "WAFFLES!"]
for(var i =0; i<3; i++){
console.log(printThree[i])
};


#4

A Function makes something reusable, so you don't have to type it again. Anything you think might ever be reusable, make it into a function.