x
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dynamic Function Assignment</title>
<script>
// Define multiple functions
function greetMorning() {
alert("Good Morning!");
}
function greetEvening() {
alert("Good Evening!");
}
// Variable to hold the current function
var currentGreetFunction;
// Function to change the current greet function
function setGreetFunction(timeOfDay) {
if (timeOfDay === "morning") {
currentGreetFunction = greetMorning;
} else if (timeOfDay === "evening") {
currentGreetFunction = greetEvening;
}
}
// Function to execute the current greet function
function executeGreetFunction() {
if (currentGreetFunction) {
currentGreetFunction();
} else {
alert("No greeting function set.");
}
}
</script>
</head>
<body>
<button onclick="setGreetFunction('morning')">Set Morning Greeting</button>
<button onclick="setGreetFunction('evening')">Set Evening Greeting</button>
<button onclick="executeGreetFunction()">Greet</button>
</body>
</html>