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 trialanthonybrackner
17,776 PointsCalling a function and storing the returned variable.
In Javascript basics, I'm being asked to call the getYear function and store the returned value in a variable called yearToday. I've tried several things and it just isn't working. I'm hoping my code is attached to this post.
function getYear() {
var year = new Date().getFullYear();
return year;
}
getyear();
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JavaScript Basics</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Steven Parker
231,248 PointsActually, ending each statement with a semicolon is considered a "best practice", whether they are needed or not.
5 Answers
Chris Jardine
13,299 PointsHi Anthony
You're nearly there, just need to store the return value in a variable called "yearToday":
function getYear() {
var year = new Date().getFullYear();
return year;
}
var yearToday = getYear();
james south
Front End Web Development Techdegree Graduate 33,271 PointsmyVar = myFunc() will result in myVar holding the value returned by the call to myFunc().
Steven Parker
231,248 PointsYou've got the "calling" part right.
When you call a function, you put parentheses after the name. So you got that part.
So when you store something in a variable, you perform an assignment operation. The assignment symbol is equal sign ("=
"). What you are assigning (the function call) goes on the right side. Where you are putting it (the variable) goes on the left side. Then all you need is the keyword "var" in front of the variable name to show that it is being created.
I'll bet you can get it now without an explicit spoiler.
Kerry Smyth
3,921 PointsNot the answer to the question but I always loved this snippet for website footers.
<script>document.write(new Date().getFullYear())</script>
Steven Parker
231,248 PointsTo avoid confusion, when contributing something that is not an answer, you can create it as a comment instead.
anthonybrackner
17,776 PointsReally appreciate it guys, I think I tried everything except for what Chris posted.
Kerry Smyth
3,921 PointsSteven Parker OK. Noted for future.
jackie antreasian
7,310 Pointsjackie antreasian
7,310 PointsHey this is what I got for ya; I want to say you don't need the semicolon when you return a value..semicolons in JS are a huge headache because sometimes you randomly don't need them and also you just forgot to store the var... so close!!! Good luck :)
function getYear(){ var year = new Date().getFullYear(); return year
};