You are probably looking for a function closure here. Using this, you will be able to store the date that you initially called the function and then each time you call it again, it will print the 6th date after the last printed 6th date.
function sixthDate() {
var curr = new Date(); // get current date
curr.setDate(curr.getDate() + 6);
return curr;
}
console.log("Normal function - ");
//prints same 6th dates everytime
console.log(sixthDate().toDateString());
console.log(sixthDate().toDateString());
const getNewSixthDate = function() {
var curr = new Date;
return function() {
curr.setDate(curr.getDate() + 6);
// returning a copy and not the reference
// so that curr doesn't get modified outside function
return new Date(curr);
}
}();
console.log("\nUsing Function Closures -");
// prints the 6th date, stores it
// and then prints the 6th date after that next time
// This process will repeat everytime
console.log(getNewSixthDate().toDateString());
console.log(getNewSixthDate().toDateString());
console.log(getNewSixthDate().toDateString());
Hope this helps !