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

Why isn't my code running?

I'm on the exercise that deals with functions.

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

getYear(); return "yearToday";

5 Answers

Erik Nuber
Erik Nuber
20,629 Points
function getYear() { 
var year = new Date().getFullYear(); 
return year; 
}

getYear(); 

Your function works if you take out the last return statement. Looks like maybe you changed variables and just didn't clean it up.

Erik-I tried the solution you posted, but it didn't run. I'll try it again.

Erik Nuber
Erik Nuber
20,629 Points

Is there more in your code than? You can copy and paste it and try in the console. It works as it is supposed to.

No.

Erik Nuber
Erik Nuber
20,629 Points

Only other thing that could be going on is if there is an html file, need to be sure that your codes is either in the <script> tag or, that your .js file is linked.

If you aren't familiar with the console, you can hit ctrl + alt + J (cmd + opt + J for mac) to bring it up in Chrome and, can paste the code there.

Erik,

I put another code in the objective. It still isn't working. I'll still keep working on it.

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

var yearToday(); getYear();

Erik Nuber
Erik Nuber
20,629 Points
function getYear() {
 var year = new Date().getFullYear(); 
return year; }

var yearToday(); 
getYear();

the line var yearToday() doesn't actually do anything. It is not a variable as it is a function call which is defined by the () brackets. There is also no function here called yearToday so it is not needed.

Without it, the code does work. I am not sure why else you could be having a problem.

function getYear() {  //2
 var year = new Date().getFullYear(); //3
return year; } //4

getYear();  //1

breaking it down line by line

1) this calls the function which does exist

2) this is defining your function and setting it up. There are no arguments being passed in.

3) this sets up both the variable year and then creates a new Date and uses the method getFullYear which grabs 2016

4) this returns year.

The only other thing that may be causing an issue depending on your goal. There is nothing actually storing the variable. You could accomplish this by doing

var currentYear = getYear()

By doing this, we are returning the information we got from the function, 2016, into the variable currentYear which could then be used for something else.