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. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    It said line 1 was missing a semi-colon so I just threw it in there and it says it's right. Can someone confirm that?

    Also the problem is on line 2. Says It's missing semi-colon.

    Thanks in advance.
     
  2. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    What's the code?
     
  3. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Oh my god I forgot to put the picture, sorry about that.
     

    Attached Files:

  4. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    you're overthinking :p

    first of all, variables and functions are very different. you can't assign a function to a variable. that's like telling somebody to hold your running. You can't do that, since running is a process.

    i believe what you are trying to do is:

    Code:
    function myFirstFunction() {
         console.log("Christopher");
    }
    
    Note that by convention, functions are named beginning with a lowercase letter, and all succeeding words are capitalized, as shown above.

    I suggest this website if you would like more information:

    http://www.w3schools.com/js/js_functions.asp
     
  5. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    The website your link leads to is good but it doesn't show me a example of what the website wants. It says I have to set a var, then make the function. If I'm saying that right. Here's the last section of what I had to do. You're throwing me off because you didn't set a var like the site wants. (well it started me off with "var" so I'm assuming).
     

    Attached Files:

  6. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    He's not throwing you off.
    If you read what the exercise actually asks for, that code is full of mistakes. You can't use it as a guide to what the site wants, the site wants you to fix it!
     
  7. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    What now... It's not telling me anything wrong and I don't see anything wrong.
     

    Attached Files:

  8. Ent

    Ent Josiah Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,408
    First, define your function.
     
  9. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Isn't that the "myfirstfunction=Christopher"?
     
  10. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    no, that would be a variable assignment. such as:

    Code:
    var someName = "Christopher";
    the following is a function declaration:

    Code:
    function myFirstFunction() {
         //some code here
    }
    Variables are used to store data, such as your name or the number 16. Functions just grouped lines of code that do something, such as a hypothetical function below:

    Code:
    function displayHelloWorld() {
         var someData = "Hello World!";
         document.write(someData);
    }
    
    That's actually a function within a function. The function I created is called displayHelloWorld, which merely creates a variable and assigns data to it. Then the document.write function (standard in javascript) takes that data and displays it on the screen. I believe in your website you use console.log instead, as it prints to the console instead.

    Remember that the equals sign (=) is merely an assignment operator. It assigns data to a variable. Functions don't store data, they only execute blocks of code.

    I would recommend checking out the link I sent you, as it should help you learn the basics of Javascript. Then I would try doing these exercises.

    EDIT: I am not familiar with the variable assignment of a function. I did some research on it. I'm not sure it's exactly a beginner friendly topic. However, I will try to explain. (Source: http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname)

    When you declare a function like this:

    Code:
    var someFunction = function() {
         //somecode
    }
    it's only understood by the computer at run-time. so if you make a call to someFunction before it is defined, it will generate an error. However, if you define a function as I mentioned above, it is understood by the computer at parse-time, which means when the computer reads it.

    Visit the link for more information.
     
  11. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    *facepalm*:p

    When you said var was a variable assignment, a "duh" came to my mind. Tried to fix it up and It said bad invocation. the error was "oops, try again", that..doesn't help me.

    What's it saying?
     

    Attached Files:

  12. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    An invocation is when you call a function. It's called 'invoking' the function. You did it incorrectly :p Your function name is myfirstfunction() correct? (Remember that programming style says you should capitalize every word after the first. So it should actually be myFirstFunction() ) But since your is called myfirstfunction(), you simply call it using that name, with no quotes. like this:

    Code:
    myfirstfunction();
    This needs to be outside of the function itself. At least for your purposes. When you get more advanced, you'll learn about calling a function from inside a function (it's called recursion). But for now, make sure you call the function outside of the function.

    Let me explain each line:

    Code:
    function myfirstfunction() { ... }
    Declares the function. Basically you're telling the computer 'when I write myfirstfunction(), do the code inside the brackets.'

    Code:
    var myfirstfunction = Christopher();
    this line is unnecessary, actually it's declaring another function (called Christopher), that does absolutely nothing since it has no brackets. Take it out. You can't define functions inside functions anyways.

    Code:
    console.log("myfirstfunction");
    I think I get what you're trying to do. You're trying to assign a variable the text, "Christopher", and then output that to the console, correct? Well, here's how you should do that.

    Code:
    var variableName = "Your Name";
    console.log(variableName);
    The last line,
    Code:
    "myfirstfunction"()
    Does NOT need the quotes. It's not a string. It's a function. It should 1) be outside the curly brackets. 2) not have quotes.

    I hope you're somewhat understanding it better :)
     
  13. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    It does actually. I don't know how you do the boxes with words in them. they say code and you write a example.how do you do that?

    Also from the 3rd and 4rth box, Do i delete var or not?

    I have to make a function. Which the "function myFirstfunction" was right?
    Then I have to make it write my name. So I have to set a var. and call up the var within the function right?. I feel my brain cells dieing form this:p.
     
  14. janikPilot

    janikPilot

    Joined:
    Nov 26, 2010
    Messages:
    141
    You have to surround the code with [ CODE ] tags. Go to the advanced editor. Type some code then highlight it and press the hashtag (#) symbol at the top of the editor. It will put [ CODE ] tags around the code and format it like that :) I put spaces in the tag so you can see it here.

    delete the third line completely.

    The
    Code:
    function myFirstFunction()
    was right. Remember that you need the parenthesis.

    You got it! You set up a var and give it a value, something you want to output to the screen. then you use the var in your output.

    In coding terms:
    Code:
    var variable = "some text";
    console.log(variable);
     
  15. turbow5

    turbow5 Thread Starter

    Joined:
    Jan 2, 2012
    Messages:
    490
    Line 2 missing semi colon. I'm not missing it, its right there! what? *facepalm* lol

    Also not yet, but I want to live the CS life.:D
     

    Attached Files:

  16. Sponsor

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