JavaScript JavaScript Basics Creating Reusable Code with Functions Returning a Value from a Function

Eric Lassard
Eric Lassard
767 Points

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 Pesantez
Antony Pesantez
1,332 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 Pesantez
Antony Pesantez
1,332 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();