Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAyaz Sadiq
Courses Plus Student 5,361 PointsCan someone please help me with an objective?
I forgot the name of the objective, but it deals with functions and calling the functions. I'm in the Java Script Basics section. Here is my code so far. I have to call the getYear function.
function getYear() {
var year = new Date().getFullYear();
return year;
}
getYear() {
var yearToday = new Date().getFullYear();
return year;
}
2 Answers
andren
28,558 PointsTo call a function you simply write the name of it with parentheses on the end like so: getYear()
If the function took arguments those would go inside the parentheses, but your function does not so you don't need to worry about that.
Edit: The task also asks you to assign the result to a variable, that is done simply by creating a variable and setting it equal to the function call, that will result in whatever is returned from the function being assigned to the variable.
Becky Steele
16,229 PointsHi Ayaz! Not sure which objective you're in, but here's an example from one in JavaScript Basics that I thought would help!
function sayHi(){
alert("Hi");
}
sayHi();
In the code block above, the function sayHi()
is created, and inside its code block, an alert with the words "Hi" would be displayed to the screen.
Anytime you want to call the function sayHi()
, you would type sayHi();
and the function would execute. This is an example of a function with no arguments only. Functions with arguments require you to pass a number of parameters to execute on.
To save the value of the result of sayHi()
, you can assign it to a variable, like this:
var response = sayHi();
Hope that helps!