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

Why does task 1 keep not passing when I am leaving the code from the previous task in place?

JavaScript getYear task 3 out of 3

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

}
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>

2 Answers

Ryan Ruscett
Ryan Ruscett
23,309 Points

This is because when you finish task 1. It checks task 1. When you go to task 2. It runs the checks for task 1 and 2. When you do task thee. It runs the task for 1, 2 and 3. But in the event where there is a compiler issue. It can't compile the code and thus not run any of the test. SO it just says test 1 failed. Cause it did, sine it couldn't compile the page to run test 1, 2 in order to get to three.

BUT you are soooo close.

You return year but you do not return the variable to a variable inside the same function.

Like this.

unction getYear (){

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

yearToday = getYear();

Thank you Ryan!

I forgot you can simply call a function in a variable.

rydavim
rydavim
18,813 Points

The return statement should be on it's own line, as the last thing in the function.

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

If you've gotten past that bit and on to task three, your call to the function should be outside of it completely. So whatever line you write for step three should go after your code from the other steps.

function getYear() {
  var year = new Date().getFullYear();
  return year;
}
// Step three goes down here, please!
// Remember that you can set a variable equal to a function call and it will hold the return value!

Awesome, thank you for the quick reply!