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 trial
Benoit Tordeurs
Full Stack JavaScript Techdegree Student 9,400 Pointsfunction followed in the code block by two other functions, with one assigned to a variable, what can I return? what ale
function getYear() {var year = new Date().getFullYear();} return year 2017;
Don Macarthur
28,430 PointsHi Benoit,
No problems at all for the help.
I'm not sure what you mean by "he does not accept the function call?" and all you posted after that as you haven't posted a link to the original challenge/video.
Can you please post a link and I'll have a look and see if I can figure out what they're looking for.
For future reference, when you're looking for help with a particular topic or challenge click on the "Get Help" button at the top right of the screen and it will add breadcrumbs to your question so other students can easily reference the challenge/quiz you are looking for help with.
Have a look at some of the other questions in the Community forums and you will see they have a group of labels after the original topic label which indicate what the question relates to. Clicking the "Get Help" button I mentioned above is what feeds this information to the Community page.
Anyway, let me know which challenge you're working on and I'll have another look for you :-)
Cheers Don
1 Answer
Don Macarthur
28,430 PointsHi Benoit,
You can return whatever you like (a string as an example), but the most logical thing to return is the variable you have declared.
So your code should look something like this:-
function getYear(year) {
var year = new Date().getFullYear(year);
return year;
}
getYear('17');
There's a few things to notice in how I wrote the code as compared to how you have originally written it:
The "return" statement needs to be inside the function - also, once the return statement has executed nothing else will run, so it needs to be last.
You will need to add an argument to your function in order to pass it to the getFullYear() method otherwise the method won't know what you are trying to find.
You need to call your function and pass it a value for the "year" argument.
Once you've got that you can call the function with any sort of date as the argument and it will return the full year eg:-
getYear('17');
getYear('1/6/17');
getYear('March 17');
All return 2017.
I hope that all makes sense for you.
Any questions, let me know :-)
Cheers Don
Don Macarthur
28,430 PointsAs an aside, the code as it's written can get confusing with the "year" variable used as an argument and also the locally declared variable.
I'd probably rewrite it as:-
function getYear(year) {
var fullYear = new Date().getFullYear(year);
return fullYear;
}
Benoit Tordeurs
Full Stack JavaScript Techdegree Student 9,400 PointsBenoit Tordeurs
Full Stack JavaScript Techdegree Student 9,400 PointsThank you so much, Don, very good improvement, I can see it & Thanks for that. However, he does not accept the function call? Once you've got that you can call the function with any sort of date as the argument and it will return the full year eg:- He does accept the argument in the function called, so I am puzzled? where to provide the argument 2017? Adding, assigning a VAR with value in the function before return? Please help Don
getYear('March 17'); All return 2017.