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

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Getting Information From a Function

Carleen Hall
seal-mask
.a{fill-rule:evenodd;}techdegree
Carleen Hall
Front End Web Development Techdegree Student 3,158 Points

Getting Information from functions

Can someone tell me what I am doing wrong, maybe I do not fully understand the return function? I am stuck on the Challenge Task 2 of 3

Inside the function's code block add this line of code var year = new Date().getFullYear(); This creates a new variable and stores the current year in it. Now, add a statement that returns this variable from the function.

Bummer! Did you use return to return the year variable like this return year;?

          function getYear() {
  var year = new Date().getFullYear();
 return "year";
}


1
function getYear() {
2
  var year = new Date().getFullYear();
3
 return "year";
4
}
          ```
Craig Fender
Craig Fender
7,605 Points

Return is used to end a function and return the sequence back to where it was originally called from. What you're returning is a string because you're wrapping it in quotes. If you want to return the variable year, then just return year without the quotes.

return year;

3 Answers

Ethan Rivas
Ethan Rivas
9,979 Points

The problem is that you're returning a string: "year" instead of just the name of the variable year

Try return year;


function getYear() {
   var year = new Date().getFullYear();

  return year;
}
Carleen Hall
seal-mask
.a{fill-rule:evenodd;}techdegree
Carleen Hall
Front End Web Development Techdegree Student 3,158 Points

Thanks for clearing that up for me. However, in the video [time stamp 107] Dave added a string/quotation marks to his return end function. How was that possible, can anyone explain?

Ethan Rivas
Ethan Rivas
9,979 Points

You can tell to your function what return, I mean, you can return a string, a integer, a boolean, etc too.

But in this case, you need return the name of the variable for return you whats inside It, or you can just simple write return = New Date... but is a good way put It inside a variable.

Sprry if I wrote something wrong im using my phone :p