Ex 10. What is the point of the empty function?


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-ASGIv/2/3?curriculum_id=506324b3a7dffd00020bf661#


I don't understand why they give an empty function. My code was accepted even though I deleted everything and started from scratch without a function. This isn't the first exercise I have come across with an empty function that seems completely unnecessary. I thought it is always better to have the shortest code possible. Can someone explain the need for the empty function?


var getToDaChoppa = false
do{
    console.log("Follow Arnie!");
}while(getToDaChoppa)


#2

If I understand you correctly, the default code that lesson starts with (or empty function as you put it) is only there to act as a template. You can start from scratch like this and it will still accept the answer if it runs without syntax error.

I hope that answers your question.


#3

Completely different exercise but I think the arguments are the same here. To be honest I don't really know why the function is part of the template if it is not necessary for the exercise. Maybe just to practice functions on a regular basis? That function really throws some people off which I cannot really understand, because functions were covered in an earlier track and the syntax:

var name = function(){

}
name()

is as easy as it can get. No parameters it's called already, all you'd need to do is to enter your code inside of the {} and it would do the exact same thing as it would do without the function. If this is not clear then it is maybe worth to bring it up for no reason to review functions, otherwise no idea. Sry if this is not a sufficient answer but I don't have a better one at the moment.


#4

That is really interesting. Here is the code I used and still got a pass. Can someone explain that? I put the false in the while and he put the false in the "var getToDaChoppa = " area

var getToDaChoppa = function(){
do {
    console.log("Maria Get to da choppa")
} while(getToDaChoppa=false)
  
};

getToDaChoppa();

#5

Well a function is a sub program that is not run on it's own but on a start command (called function call). Which has the advantage that it can be called whenever you like to and as often as you like to. So whether you write your code here:

var getToDaChoppa = function(){
//code
};

getToDaChoppa();

or without the function doesn't make a difference when you only call the function once. But If you'd call it twice with this framework you'd just need another getToDaChoppa(); whereas @thejakub would need to rewrite his full code.

The problem with your code is that it would, even with the function, only runs once. Test it, just add another getToDaChoppa(); and what you should see is an error telling you that getToDaChoppa is not a function. And the error message is true because when you would replace the second getToDaChoppa(); with a console.log(getToDaChopp); you'd see that it actually has a value of false.

And as you might already guess this comes from your condition:

while(getToDaChoppa=false)

So first of all never use the function name as a variable inside of the function unless you explicitly know what you're doing. Because, as you could see by applying the steps mentioned, you could overwrite the function by using it. It also makes no sense to compare a function to a boolean, does it?

So first thing to do: Get a different variable for your condition. And the other thing that is wrong about this condition is that you're using the = operator which makes assignment, it "works" here out of mostly luck because

name = value

has as a statement a value of value, so in this case false. So better stick with a comparison operator like == or ===.

Hope this helps.


#6

This makes no sense to me at all. I just don't get it.


#7

I tried this way and i got stuck, the browser freezes and i don't know why

CODE
var getToDaChoppa = function(){
// Write your do/while loop here!\
var num = true;
do{
console.log("teste");
} while(num = true){
console.log("teste2");
num = false;
}
};

getToDaChoppa();


#8

Similar to what I wrote earlier here:

but in your case you use name=true instead of name=false which is even worse because it translates to while(true) which is an infinite loop.


#9

I didnt get very well, if i put num = true outside of the function change anything?


#10

The point is that you need to distinguish the types of equal signs:
One equal sign = means: Store the value on the right inside of the variable on the left.
Two equal signs == mean: Compare the values on the left and right of it, true if they are equal false otherwise.
Three equal signs === mean: Compare the values and types on the left and right of it, true if they are both equal false otherwise.

So using the single equal sign is the wrong one in the first place. Now it gets even worse because as I tried to explain in the other post expressions can have values of their own. For example if you take an assignment expression like:

var name;
name = "rubymaster64771";

then the line name = "rubymaster64771"; will have a value of "rubymaster64771". Now if you compare this to your condition of while(num = true) you can see that this has 2 effects:

  1. num will get a new value of true
  2. the whole statement num = true has a value of true

and so you end up with an effective statement like this:

while(true)

which is the classical infinite loop with no chance to stop it but using the break; keyword. Hope this makes it a little clearer.


#11

Yes, thanks for your patience and explanation, the sign of '=' i understood at the first time, and now i understood where was wrong, thnks very much