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

i don't know whats wrong

it says task one no longer passing

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

Hey

I presume you are on task two? Get rid of the alert and just change the return statement to

return year;

This returns the variable year not the string "return year"

Hope this helps

Happy coding

Paul

Fridrik Juliusson
Fridrik Juliusson
12,113 Points

Change your function to this:

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

Basically, you create a variable called "year" inside the function. And to return the value of that variable, you write the name of the variable, without quote marks. If you add quote marks, it will be interpreted as the word, or string, "year" rather than the variable with that name.

Also, in the alert, you need to pass in the function getYear(). This will return the value of the variable "year" to the alert, and it should print out the value of the variable in the alert box.

Hope this helps!

EDIT: I just looked at the challenge and noticed that there is no task to create an alert. So you can just delete the last line that says "alert (getYear());" However, if you want to use the function in an alert, then the code I wrote above should do the job.