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 Returning a Value from a Function

Call the getYear function: store the returned value of the function in a new variable named yearToday.

Call the getYear function: store the returned value of the function in a new variable named yearToday.

OK, so what's the problem? Let's see some code.

7 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Katrina;

If you post your code when asking questions it is greatly helpful.

With that in mind it sounds like you are on Task 3 of 3, in which case your starting code for that task should look similar to:

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

Correct?

If you are unclear as to what that function does or how it works, take another look at the videos.

Task 3, which is where it sounds like you are having an issue, states:

Call the getYear function: store the returned value of the function in a new variable named yearToday.

So, you just created the getYear function and do you recall from the videos how to call a function? If not I would recommend going back and watching again, but one can call the function getYear with:

getYear();

In the case of this challenge, our function will return the current year, so as of today it would be a value of 2014.

The task also asks to store that value into a new variable named yearToday. So we need to put into our code:

yearToday = ???; // Need to set the value of ??? to something

That should point you in the right direction, post back if you still are stuck.

Ken

This code should work. At least it did for me.

getYear();
var yearToday = getYear();

Good Luck

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

var yearToday = getYear();

Jaime Sánchez Blanco
Jaime Sánchez Blanco
16,894 Points

Hi, this worked for me:

function getYear () { var year = new Date() .getFullYear(); return year; } var yearToday = getYear(); if (getYear = = = true) { alert(true)}

Ajay pratap Singh
Ajay pratap Singh
5,056 Points

this is not Worked For me There was an error with your code: SyntaxError: Parse error

Sorry. I work it out for myself. Thank you for your help. New to the whole process.

Thank u

Task 3 had me stumped. Parse syntax errors, grrr. I'd recommend re-watching the video. Soon as I did, setting up a function within a variable clicked. Below code worked for me. Hope it helps:

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