%%html
<!-- HTML output div -->
<div id="message"></div>
<script>
function runWhenDOMLoaded() {
// Example choices
let appleColor = "green"; // Change this to "green" to test different outcomes
let eatApple = "no"; // Change this to "no" to test different outcomes
const messageElement = document.getElementById("message");
function displayMessage(message) {
console.log(message); // Log to console
messageElement.textContent = message; // Display on the webpage
}
// Check the color of the apple chosen by the user
if (appleColor === "green") {
// If the apple is red, check if the user wants to eat it
if (eatApple === "no") {
displayMessage("Lmao, the apple you ate was poisonous. You're dead :D");
} else {
displayMessage("You decided not to eat the red apple (You're boring -_-).");
}
} else if (appleColor === "green") {
// If the apple is green, check if the user wants to eat it
if (eatApple === "no") {
displayMessage("You ate the green apple.");
} else {
displayMessage("You decided not to eat the green apple (You're boring -_-).");
}
} else {
// If the user enters an invalid color, display an error message
displayMessage("Invalid answer. Please choose 'red' or 'green'.");
}
}
// Ensure the function runs only after the page loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', runWhenDOMLoaded);
} else {
runWhenDOMLoaded();
}
</script>
%%js
<div id="message"></div>
<script>
function runWhenDOMLoaded() {
let isHungry = true;
// Define ingredients as a JSON object
const ingredients = {
"bread": false,
"cheese": false,
"tomato": true,
};
const messageElement = document.getElementById("message");
function displayMessage(message) {
console.log(message); // Log to console
messageElement.textContent = message; // Display on the webpage
}
// Check if essential ingredients are available
if (isHungry) {
if (ingredients.bread && ingredients.cheese) {
displayMessage("You can make a cheese sandwich at home.");
} else if (ingredients.bread) {
displayMessage("You only have bread, maybe make toast.");
} else {
displayMessage("You should order food since you don't have enough ingredients.");
}
} else {
displayMessage("You aren't hungry. Maybe meal-prep for later if you had ingredients.");
}
}
// Ensure the function runs only after the page loads
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', runWhenDOMLoaded);
} else {
runWhenDOMLoaded();
}
</script>
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>