how to get last weekday for a given date

I am trying to get the last weekday for a given date in my google sheets for which I have written the below custom function. If it is a saturday or sunday then i want to get the last friday which I assume is a weekday (leaving out edge cases where friday could be a holiday as well)

This does not work and I get the below ERROR. Can you please let me know where I am going wrong?

“An error occurred in the script, but there is no error code: Error: Cannot return an invalid date.”
In my

In my google sheets I am trying to call it as below
enter image description here

function lastBusinessDay(passedDate) {
  mydate = new Date(passedDate);
  console.log('date recieved ' + mydate);
  if(mydate.getDay()==1)
    return new Date(today.getFullYear(), today.getMonth(), today.getDate() - 2);
  else if(mydate.getDay()==6)
    return new Date(today.getFullYear(), today.getMonth(), today.getDate() - 1);
  else
    return mydate;  
  
}