generated from nighthawkcoders/student_2025
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lara3333
committed
Nov 4, 2024
1 parent
9ba78af
commit ef4f813
Showing
3 changed files
with
566 additions
and
0 deletions.
There are no files selected for viewing
201 changes: 201 additions & 0 deletions
201
_notebooks/CSSE/Lessons/Nested_Conditionals/2024-10-26-nested_conditionals.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "raw", | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "raw" | ||
} | ||
}, | ||
"source": [ | ||
"---\n", | ||
"comments: true\n", | ||
"layout: post\n", | ||
"title: Nested Conditionals with booleans Javascript Lesson\n", | ||
"description: Nested conditionals with booleans in Javascript are condition statements placed inside condition statements, allowing multiple layers of decision making.\n", | ||
"type: ccc\n", | ||
"author: Anika Marathe\n", | ||
"\n", | ||
"\n", | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h2><span style=\"font-family: Ariel; color:#94c4ff\">Booleans with Nested Conditionals</span></h2>\n", | ||
"\n", | ||
"\n", | ||
"<p><span style=\"font-family: Arial\">A <b>boolean</b> is a data type that represents one of two possible values: <code>true</code> or <code>false</code>. Booleans are useful for decision-making in code, as they let us determine if specific conditions are met. For example, if we want to check if someone meets the age requirement for a concert or has a ticket, we might represent these conditions as booleans.</span></p>\n", | ||
"\n", | ||
"<p><span style=\"font-family: Arial\">A <b>nested boolean</b> is simply a boolean condition used inside another condition. When using <b>nested conditionals</b> (conditions inside other conditions), we can build complex decision-making logic. For example, we might check first if the person is old enough, and if they are, then check if they have a ticket. This kind of logic allows us to handle multiple conditions in a structured way.</span></p>\n", | ||
"\n", | ||
"<p><span style=\"font-family: Arial\">Here’s an example:</span></p>\n", | ||
"\n", | ||
"```javascript\n", | ||
"let isOldEnough = true; // Boolean indicating if the person meets the age requirement\n", | ||
"let hasTicket = false; // Boolean indicating if the person has a ticket\n", | ||
"\n", | ||
"if (isOldEnough) { // Check if the person meets the age requirement\n", | ||
" if (hasTicket) { // Nested condition to check if they have a ticket\n", | ||
" console.log(\"You can attend the concert.\");\n", | ||
" } else {\n", | ||
" console.log(\"You need a ticket to attend the concert.\");\n", | ||
" }\n", | ||
"} else {\n", | ||
" console.log(\"You do not meet the age requirement for this concert.\");\n", | ||
"}\n", | ||
"```\n", | ||
"\n", | ||
"---\n", | ||
"\n", | ||
"1. The code below checks if you can cook a meal at home. \n", | ||
"2. It uses the presence of hunger, and ingredients, to determine if cooking a meal is possible at home.\n", | ||
"3. It uses booleans in the code\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "html" | ||
} | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<!-- HTML output div -->\n", | ||
"<div id=\"message\"></div>\n", | ||
"\n", | ||
"<script>\n", | ||
" function runWhenDOMLoaded() {\n", | ||
" let isHungry = true;\n", | ||
"\n", | ||
" // Define ingredients as a JSON object\n", | ||
" const ingredients = {\n", | ||
" \"bread\": true,\n", | ||
" \"cheese\": false,\n", | ||
" \"tomato\": false\n", | ||
" };\n", | ||
"\n", | ||
" const messageElement = document.getElementById(\"message\");\n", | ||
"\n", | ||
" function displayMessage(message) {\n", | ||
" console.log(message); // Log to console\n", | ||
" messageElement.textContent = message; // Display on the webpage\n", | ||
" }\n", | ||
"\n", | ||
" // Check if essential ingredients are available\n", | ||
" if (isHungry) {\n", | ||
" if (ingredients.bread && ingredients.cheese) {\n", | ||
" displayMessage(\"You can make a cheese sandwich at home.\");\n", | ||
" } else if (ingredients.bread) {\n", | ||
" displayMessage(\"You only have bread, maybe make toast.\");\n", | ||
" } else {\n", | ||
" displayMessage(\"You should order food since you don't have enough ingredients.\");\n", | ||
" }\n", | ||
" } else {\n", | ||
" displayMessage(\"You aren't hungry. Maybe meal-prep for later if you had ingredients.\");\n", | ||
" }\n", | ||
" }\n", | ||
"\n", | ||
" // Ensure the function runs only after the page loads\n", | ||
" if (document.readyState === 'loading') {\n", | ||
" document.addEventListener('DOMContentLoaded', runWhenDOMLoaded);\n", | ||
" } else {\n", | ||
" runWhenDOMLoaded();\n", | ||
" }\n", | ||
"</script>\n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"%%html\n", | ||
"<!-- HTML output div -->\n", | ||
"<div id=\"message\"></div>\n", | ||
"\n", | ||
"<script>\n", | ||
" function runWhenDOMLoaded() {\n", | ||
" let isHungry = true;\n", | ||
"\n", | ||
" // Define ingredients as a JSON object\n", | ||
" const ingredients = {\n", | ||
" \"bread\": true,\n", | ||
" \"cheese\": false,\n", | ||
" \"tomato\": false\n", | ||
" };\n", | ||
"\n", | ||
" const messageElement = document.getElementById(\"message\");\n", | ||
"\n", | ||
" function displayMessage(message) {\n", | ||
" console.log(message); // Log to console\n", | ||
" messageElement.textContent = message; // Display on the webpage\n", | ||
" }\n", | ||
"\n", | ||
" // Check if essential ingredients are available\n", | ||
" if (isHungry) {\n", | ||
" if (ingredients.bread && ingredients.cheese) {\n", | ||
" displayMessage(\"You can make a cheese sandwich at home.\");\n", | ||
" } else if (ingredients.bread) {\n", | ||
" displayMessage(\"You only have bread, maybe make toast.\");\n", | ||
" } else {\n", | ||
" displayMessage(\"You should order food since you don't have enough ingredients.\");\n", | ||
" }\n", | ||
" } else {\n", | ||
" displayMessage(\"You aren't hungry. Maybe meal-prep for later if you had ingredients.\");\n", | ||
" }\n", | ||
" }\n", | ||
"\n", | ||
" // Ensure the function runs only after the page loads\n", | ||
" if (document.readyState === 'loading') {\n", | ||
" document.addEventListener('DOMContentLoaded', runWhenDOMLoaded);\n", | ||
" } else {\n", | ||
" runWhenDOMLoaded();\n", | ||
" }\n", | ||
"</script>\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<h3><span style=\"font-family: Ariel; color:#94c4ff\">Popcorn Hack 2</span></h3>\n", | ||
"\n", | ||
"1. Open two new code cells and set them to javascript\n", | ||
"2. Copy the code example from above javascript cell into both the new code cells \n", | ||
"3. Change the first new code cell to print \"You aren't hungry. Maybe meal-prep for later if you had ingredients.\" \n", | ||
"4. Change the second new code cell to print \"You can make a cheese sandwich at home.\" " | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.