Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

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


(!)

janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
13-Aug-2012, 06:46 AM #16
Quote:
Originally Posted by turbow5 View Post
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.
You have an extra parenthesis on that line for no reason, where did that come from? 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

Last edited by janikPilot; 13-Aug-2012 at 09:41 AM..
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
13-Aug-2012, 03:37 PM #17
I remember how to do var. Least I think I do.

This is how you do it right?
Attached Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-javascript-code.png  
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
13-Aug-2012, 03:49 PM #18
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)

Last edited by janikPilot; 13-Aug-2012 at 03:54 PM..
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
14-Aug-2012, 04:05 AM #19
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 Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-javascript-code.png  
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,290 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
14-Aug-2012, 04:13 AM #20
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.
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
14-Aug-2012, 06:12 AM #21
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...
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,290 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
14-Aug-2012, 06:17 AM #22
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.
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
14-Aug-2012, 07:08 AM #23
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........... 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.
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
14-Aug-2012, 07:22 AM #24
Quote:
Originally Posted by turbow5 View Post
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........... 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.
Oh it's not that bad

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.
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
16-Aug-2012, 03:44 PM #25
School started and this will blow my brains out .

Is a function a "must-know" thing? from this exercise it seems I just set a variable and called it up.
Attached Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-javascript-code.png  
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,290 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
16-Aug-2012, 04:58 PM #26
Quote:
Originally Posted by turbow5 View Post
Is a function a "must-know" thing?
Yes. I would go as far as to say you cannot use Javascript without a solid understanding of functions.

Quote:
from this exercise it seems I just set a variable and called it up.
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.
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
16-Aug-2012, 07:06 PM #27
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.
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
16-Aug-2012, 07:31 PM #28
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?
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
16-Aug-2012, 08:23 PM #29
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
turbow5's Avatar
turbow5 turbow5 is online now
Computer Specs
Member with 482 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
16-Aug-2012, 10:03 PM #30
I will remember that, won't be for a while though.
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
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.


Tags
javascript

(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑

Content Relevant URLs by vBSEO 3.3.2