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

Having Issues With The Calling Functions and Storing In Vars

Hey guys, Back again with a brand new issue storing vars and functions let me know if you guys can help!

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

var getYear =  return 
index.html
<!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>

1 Answer

Antony .
Antony .
2,824 Points
function getYear(){
  var yearToday = new Date().getFullYear();
  return new Date().getFullYear(); //on this line you don't need to repeat the Date object because you already declared it on your variable called "yearToday". So you can change this to, "return yearToday;"
}

var getYear =  return //this will give you a syntax error because return isn't a valid value for a variable ("i think"). So you can change this to, "var getYear = getYear();", and then underneath that you can log this variable with console.log

So it should be written like this:

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

var getYear = getYear();
console.log(getYear);

I don't quite remember this track but i hope this works for you!

Antony .
Antony .
2,824 Points

Actually I just reviewed the quiz you were on. Everything is the same except the variable "year" inside the function. Your answer should look like this:

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

var yearToday = getYear();