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

Rene Guzman
Rene Guzman
8,587 Points

I'm completely stuck on this. I feel like I've tried every possible way and all I get is Task 1 no longer working

I'm completely stuck on this. I feel like I've tried every possible way and all I get is Task 1 no longer working. I've watched the 2 vids from the section repeatedy and I just can't figure out what else to try

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

3 Answers

Steven Ang
Steven Ang
41,751 Points

First, create a function called getYear() which you've already done, good job. Second, copy and paste the code given to you and return the variable. Finally, call the function and store it in a variable named "yearToday".

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

yearToday = getYear();
james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are not setting the value of the yearToday variable to the return value of the function. when a function is called, it runs and any return value is returned at the point in the code the function was called. this can then be assigned using the assignment = operator to a variable. so you don't need the brackets for a new block, just var myVar = myFunc() like that.

Rene Guzman
Rene Guzman
8,587 Points

Thank you guys for your answers. After seeing your replies and rethinking what I was doing I realized I was overcomplicating what had to be done lol Thanks again