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?


(!)

turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
06-Aug-2012, 05:29 PM #1
Solved: Javascript, defining functions. This is right isn't it?
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.
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,277 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
06-Aug-2012, 05:52 PM #2
What's the code?
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
06-Aug-2012, 05:56 PM #3
Oh my god I forgot to put the picture, sorry about that.
Attached Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-script.png  
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
06-Aug-2012, 06:07 PM #4
you're overthinking

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

Last edited by janikPilot; 06-Aug-2012 at 08:46 PM..
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
06-Aug-2012, 11:02 PM #5
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 Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-script.png  
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,277 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
07-Aug-2012, 01:53 AM #6
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!
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
07-Aug-2012, 10:15 AM #7
What now... It's not telling me anything wrong and I don't see anything wrong.
Attached Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-script.png  
Ent's Avatar
Ent   (Josiah) Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 5,277 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
07-Aug-2012, 10:17 AM #8
First, define your function.
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
07-Aug-2012, 06:42 PM #9
Isn't that the "myfirstfunction=Christopher"?
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
07-Aug-2012, 08:09 PM #10
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/3...n-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.

Last edited by janikPilot; 07-Aug-2012 at 08:16 PM..
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
11-Aug-2012, 05:28 PM #11
*facepalm*

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 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
11-Aug-2012, 05:48 PM #12
An invocation is when you call a function. It's called 'invoking' the function. You did it incorrectly 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
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
12-Aug-2012, 08:32 PM #13
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.
janikPilot's Avatar
janikPilot   (Michael) janikPilot is offline
Computer Specs
Member with 141 posts.
 
Join Date: Nov 2010
Location: South FL
Experience: Advanced
12-Aug-2012, 09:34 PM #14
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);

Last edited by janikPilot; 12-Aug-2012 at 11:03 PM..
turbow5's Avatar
turbow5 turbow5 is offline
Computer Specs
Member with 477 posts.
THREAD STARTER
 
Join Date: Jan 2012
Experience: Intermediate
13-Aug-2012, 01:05 AM #15
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.
Attached Thumbnails
Solved: Javascript, defining functions. This is right isn't it?-javascript-code.png  
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 ↑