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

Samuel Israelsen
Samuel Israelsen
4,965 Points

having trouble with returning functions

In this coding challenge I'm supposed to call the getYear function and put the returned value into another variable called yearToday. I'm having a hard time knowing where to put the code or if I'm even putting the right code down at all! Could someone please help me through this?

script.js
function getYear () {
  var year = new Date().getFullYear();
  return year;
  getYear ();
}
var yearToday(getYear());
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

Steven Parker
Steven Parker
229,783 Points

Storing something into a variable is called assignment.

The assignment operator is an equal-sign ("="). The variable name will be on the left, and the value being assigned (in this case the function call) will be on the right.

var yearToday = getYear();

Also, I noticed you have a call to the function getYear() inside the function itself. Since it was placed after the return, it does nothing, which is fortunate because otherwise it would have caused infinite recursion consuming memory until an error occurred.

Samuel Israelsen
Samuel Israelsen
4,965 Points

Thank you so much! I knew it would be something as simple as that. Thanks for your help