[Solved] Why isn’t this function working right?


The code itself in your first attempt works, apart from the last line. Right now you just return the first date and ignore the second one and the combined string of dates. I expect you intended to return the string which shows both dates.

return both;

should solve your problem. Demo:

function todaysDate() {
  var today = new Date();
  var date = today.getFullYear();
  var date2 = today.getFullYear()+2;
  var both = date +"-"+date2;
  return both;
}
console.log(todaysDate());

Possibly it was just a typo or oversight.

I would also consider giving your function a better name, because it doesn’t actually return today’s date.

solved Why isn’t this function working right?