1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Javascript, defining functions. This is right isn't it?

Discussion in 'Web Design & Development' started by turbow5, Aug 6, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    You have an extra parenthesis on that line for no reason, where did that come from? :p Errors are not always what the compiler tells you they are. It just takes its best guess at what the error is. Your actual error is the extra parenthesis.

    But you are correct, just be careful when you name your variables. Don't give it that name, it's confusing. Name it something that doesn't confuse it with a function. The point is to name it something descriptive, like for instance since it contains your name, you name the variable, 'name'.

    That's a website I'm trying to start, hopefully more people agree with you :D
     
  2. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    I remember how to do var. Least I think I do.

    This is how you do it right?
     

    Attached Files:

  3. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    Yes! You almost have it!

    When you use the variable, like in a function, you must call it without quotes. Consider the following:

    Code:
    var name="Michael";
    console.log(name);
    Will display my name, Michael, to the screen. However,

    Code:
    var name="Michael";
    console.log("name");
    Will display the word name to the screen. In the second example, you are just passing the data "name" to the function, instead of the variable called name which holds the data of my name (Michael).

    Note that the way you defined the function there is the correct way, but it seems your tutorial is saying it is not. I believe they want you to define it the other way, which is like
    Code:
    var myFirstFunction = function() {
    
    }
    Which is very confusing to a beginner. Understand that for your purposes,
    Code:
    function myFirstFunction() {
    
    }
    and

    Code:
    var myFirstFunction = function() {
    
    }
    Are exactly the same thing, just different ways of writing it. (They differ slightly, but since you are just starting out and the tutorial requires it, pretend they do the same thing. You can see my earlier post for their differences)
     
  4. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    So you are saying that I made and called a function correctly? If you are then it'll be buried in my brain.

    I tried the new way you wanted me to make a function, but there's a error on line one. Says the ; but I know it isn't that.
     

    Attached Files:

  5. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    It is a problem with the semicolon. Remember that the semicolon is to Javascript what the Full stop or period is to us.

    In essence you've written
    And now I'm going to teach you how to.

    Remove that semicolon.
     
  6. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Thank you for telling me that actually. The website never told me that the ; was a period to us.

    So I took that out and this is what I'm left with.

    var myFirstfunction=function (){
    console.log ("christopher");
    christopher ();}

    Line 3 is my problem now..hmm...missing semi colon. I don't think that could be it...I'm stairing at this trying to figure it out...
     
  7. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    Line 3 tells it to execute the function called cristopher. What function would that be? ;)

    Also, it's inside the function you're working with, which means that once you do fix it the function will end up calling itself every time it runs. That has some rather unfortunate consequences, as the program can never complete (as it can't escape from that function) and will end up crashing.
     
  8. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Ok so line 3 execute the function Christopher. But the var makes myFirstfunction=function. Then from the prevoius section I was told to tell the console..which is console.log..which "christopher" is what the function is suppose to display right? so..:confused:...:confused:...:confused:... I call it up by myFirstfunction..right? But If I do that then I'm missing semi colon.

    No wonder you gotta go to college for this stuff. It gets hard fast.
     
  9. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    Oh it's not that bad :p

    No, you're doing it right. If the function that prints "Christopher" to the console is called myFirstFunction(), then that's the name you use to call it OUTSIDE of the function itself. You were calling it from the inside.

    So close the function (end it with a } bracket) and on the next line type myFirstFunction(); Remember to place the semicolon at the end of the statement.
     
  10. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    School started and this will blow my brains out :p.

    Is a function a "must-know" thing? from this exercise it seems I just set a variable and called it up.
     

    Attached Files:

  11. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,407
    Yes. I would go as far as to say you cannot use Javascript without a solid understanding of functions.

    There is a fundamental difference between a function and a variable.
    A variable refers to a piece of information, 96, "Christopher" or FALSE for example.
    A function contains instructions for the computer.
    Thinking about functions as just another variable is like thinking of a recipe as an ingredient.
     
  12. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    The thing that is really confusing him though is this tutorial, since it introduced the var declaration of a function WAY too early. The first way you should learn how to program a function is simply "function name()", not "var name = function()" since that is extremely confusing, even for me.

    turbow5, I don't know what else we can tell you. We've explained everything.
     
  13. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Yeah I had a feeling you two did. I'm just going to take a break from it. Come back later, and see if I can reconfigure it in my skull.

    Also I'm youtube'ing this till I get it. I really do appiciete you two for helping though.

    Janikpilot did you say from post #17 that I did the function right in a way?
     
  14. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    yes, you did the function declaration totally correct. Only problem with your code was you used quotes around the variable name.

    No problem, post again or just PM me if you need help later :)
     
  15. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    I will remember that, won't be for a while though.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1064098