Skip to content

Commit 38f016c

Browse files
authored
Merge pull request #6 from S2P2/push-pnmzpoqzsvlv
Push pnmzpoqzsvlv
2 parents 20cc9fb + 8f3ac39 commit 38f016c

File tree

5 files changed

+256
-6
lines changed

5 files changed

+256
-6
lines changed

src/SUMMARY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
- [Cooking in Batches (Collections & Loops)](concepts/module-4/README.md)
2525
- [The Pantry (Arrays & Lists)](concepts/module-4/arrays-and-lists.md)
2626
- [Stir Until Combined (For Loops)](concepts/module-4/for-loops.md)
27-
- [Wait for the Water to Boil (While Loops)]()
28-
- [Grocery List (Dictionaries / Name-Value Pairs)]()
27+
- [Wait for the Water to Boil (While Loops)](concepts/module-4/while-loops.md)
28+
- [The Recipe Card (Name-Value Pairs)](concepts/module-4/key-values.md)
2929
- [Organizing Your Kitchen (Functions & Imports)](concepts/module-5/README.md)
3030
- [Kitchen Stations (Functions)](concepts/module-5/functions.md)
3131
- [Passing Ingredients (Function Parameters)](concepts/module-5/function-parameters.md)
3232
- [Returning the Dish (Function Returns)](concepts/module-5/function-returns.md)
33-
- [Kitchen Gadgets (Using External Code)](concepts/module-5/imports.md)
33+
- [Kitchen Gadgets (Imports)](concepts/module-5/imports.md)
3434

3535
# First Project
3636

src/concepts/module-2/working-with-text.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Chopping and Combining (Working with Text)
22

3-
Working with numbers is about calculation, but working with text—or **strings**—is about communication. The most common task you'll perform with strings is combining them to create new, more meaningful messages.
3+
Working with numbers is about calculation, but working with text—or **strings**—is about communication. You'll constantly need to prepare your text to be shown to a user, whether it's a welcome message, a menu item, or an error warning.
44

5-
This process of joining strings together is called **concatenation**. 🔗
5+
Let's look at the three most common ways you'll work with strings: combining them, inspecting their properties, and changing their style.
6+
7+
### 1. Combining: "Pen Pineapple Apple Pen"
8+
9+
The most frequent task you'll perform with strings is joining them together to create new, more meaningful messages. This process is called **concatenation**. 🔗
610

711
It's like connecting two train cars to make a longer train. In many languages, you can use the same `+` symbol you used for addition to concatenate strings.
812

@@ -34,4 +38,74 @@ You got [Pen Pineapple Apple Pen](https://www.youtube.com/watch?v=NfuiB52K7X8)!
3438

3539
Notice that we had to add a space `" "` in the middle. The computer is extremely literal; it only combines *exactly* what you give it. Without that space, the result would have been `"Pen PineappleApple Pen"`.
3640

37-
This is a fundamental building block. You'll use it to create dynamic text (`"Order: " + orderItem`), generate reports, or display any kind of organized text to a user.
41+
### 2. Inspecting: "Is the Username Too Long?"
42+
43+
Sometimes you don't need to change the text, but you need to get information *about* it. The most common piece of information you'll need is its **length**.
44+
45+
This is useful for checking things like, "Is this username less than 15 characters?" or "Will this menu item name fit on the display?"
46+
47+
<!-- langtabs-start -->
48+
49+
```py
50+
# In Python, we use the len() function to get the length of a string.
51+
menu_item = "Extra Cheesy Supreme Pizza"
52+
item_length = len(menu_item)
53+
54+
print("Menu Item:", menu_item)
55+
print("Character Count:", item_length)
56+
57+
# Now we can use this information in a decision!
58+
if item_length > 20:
59+
print("Warning: This name might be too long for the menu board!")
60+
61+
```
62+
63+
```js
64+
// In JavaScript, we use the .length property to get the length of a string.
65+
let menuItem = "Extra Cheesy Supreme Pizza";
66+
let itemLength = menuItem.length;
67+
68+
console.log("Menu Item:", menuItem);
69+
console.log("Character Count:", itemLength);
70+
71+
// Now we can use this information in a decision!
72+
if (itemLength > 20) {
73+
console.log("Warning: This name might be too long for the menu board!");
74+
}
75+
```
76+
<!-- langtabs-end -->
77+
78+
### 3. Styling: "Shouting the Daily Special"
79+
80+
Often, you'll need to change the case of a string for formatting purposes. For example, you might want to display a heading in all capital letters or normalize user input by converting it all to lowercase.
81+
82+
Think of it like deciding how to write something on a menu board. Do you want to SHOUT IT, or write it in normal case?
83+
84+
<!-- langtabs-start -->
85+
86+
```py
87+
daily_special = "Classic Burger with Fries"
88+
89+
# To make a big headline for the menu board, we use .upper()
90+
shouted_special = daily_special.upper()
91+
print(shouted_special) # Displays: CLASSIC BURGER WITH FRIES
92+
93+
# To store it in a database consistently, we might use .lower()
94+
normalized_special = daily_special.lower()
95+
print(normalized_special) # Displays: classic burger with fries
96+
```
97+
98+
```js
99+
let dailySpecial = "Classic Burger with Fries";
100+
101+
// To make a big headline for the menu board, we use .toUpperCase()
102+
let shoutedSpecial = dailySpecial.toUpperCase();
103+
console.log(shoutedSpecial); // Displays: CLASSIC BURGER WITH FRIES
104+
105+
// To store it in a database consistently, we might use .toLowerCase()
106+
let normalizedSpecial = dailySpecial.toLowerCase();
107+
console.log(normalizedSpecial); // Displays: classic burger with fries
108+
```
109+
<!-- langtabs-end -->
110+
111+
These three operations—combining, checking length, and changing case—are the essential tools in your text-handling toolkit. You'll use them constantly to build dynamic, readable, and user-friendly programs.

src/concepts/module-3/if-else.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,44 @@ if (internalTemp >= 165) {
3737
<!-- langtabs-end -->
3838

3939
Now, try changing the `internal_temp` (or `internalTemp`) to something less than `165`, like `150`, and run the code again. You'll see the program take the other path and execute the code inside the `else` block instead. This is how you make your programs dynamic and responsive, just like a chef adjusting their cooking based on observations!
40+
41+
## The Goldilocks Problem (Multi-Path Decisions)
42+
43+
What if you have more than two options? This is like Goldilocks tasting the porridge: one bowl is too hot, one is too cold, and one is just right. An `if/else` statement only gives you two paths, but you can add more paths using `else if` (or `elif` in Python).
44+
45+
This lets you chain conditions together:
46+
47+
1. **`if`**: Checks the first condition. If it's `true`, the code runs, and the chain is exited.
48+
2. **`else if`**: If the first condition was `false`, this next condition is checked. You can have as many `else if` blocks as you need.
49+
3. **`else`**: If none of the above conditions were `true`, this code runs as a fallback.
50+
51+
Let's see how to handle porridge that is too hot, too cold, or just right.
52+
53+
<!-- langtabs-start -->
54+
55+
```py
56+
porridge_temp = 75
57+
58+
if porridge_temp > 85:
59+
print("This porridge is too hot!")
60+
elif porridge_temp < 65:
61+
print("This porridge is too cold!")
62+
else:
63+
print("This porridge is just right.")
64+
```
65+
66+
```js
67+
let porridgeTemp = 75;
68+
69+
if (porridgeTemp > 85) {
70+
console.log("This porridge is too hot!");
71+
} else if (porridgeTemp < 65) {
72+
console.log("This porridge is too cold!");
73+
} else {
74+
console.log("This porridge is just right.");
75+
}
76+
```
77+
78+
<!-- langtabs-end -->
79+
80+
With this structure, you can create clear and effective logic for any number of choices, ensuring your program always knows the right step to take.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# The Recipe Card (Name-Value Pairs)
2+
3+
So far, we've used lists (the "Pantry") to store collections of items in a specific order. This is great for a sequence of recipe steps or a list of ingredients, where you access items by their position (`0`, `1`, `2`, etc.).
4+
5+
But what if you need to store information that isn't a sequence, but a collection of labeled properties? This is where a **dictionary** (or **map**, or **object**, depending on the language) comes in. It's a collection that stores data not by its position, but by a unique **name**.
6+
7+
Think of it as a **Recipe Card**... To find out the cook time, you don't look at the third line; you look for the label that says "Cook Time" and read the value next to it.
8+
9+
This `name: value` pairing is the heart of a dictionary. In programming, the 'name' is officially called a **key**.
10+
11+
- **Key:** A unique identifier (like a label on the recipe card). It's almost always a string.
12+
- **Value:** The data associated with that key. It can be any data type: a string, a number, a boolean, or even another list or dictionary!
13+
14+
Let's create a recipe card for a classic dish.
15+
16+
<!-- langtabs-start -->
17+
18+
```python
19+
# A dictionary representing a recipe.
20+
# Notice the {curly braces} and the "key": value syntax.
21+
recipe = {
22+
"name": "Classic Tomato Soup",
23+
"servings": 4,
24+
"cook_time_minutes": 30,
25+
"is_vegetarian": True
26+
}
27+
28+
# --- Accessing data ---
29+
# To get a value, you use its key inside [square brackets].
30+
print("Dish Name:", recipe["name"])
31+
print("Serves:", recipe["servings"])
32+
33+
# --- Updating data ---
34+
# You can change a value by assigning a new one to its key.
35+
print("\nOops, we have more guests! Let's double the recipe.")
36+
recipe["servings"] = 8
37+
print("Now serves:", recipe["servings"])
38+
39+
# --- Adding new data ---
40+
# You can add a new key-value pair just by assigning it.
41+
recipe["difficulty"] = "Easy"
42+
print("Difficulty:", recipe["difficulty"])
43+
```
44+
45+
```javascript
46+
// An object representing a recipe.
47+
// Notice the {curly braces} and the key: value syntax.
48+
let recipe = {
49+
name: "Classic Tomato Soup",
50+
servings: 4,
51+
cookTimeMinutes: 30,
52+
isVegetarian: true
53+
};
54+
55+
// --- Accessing data ---
56+
// To get a value, you can use dot notation (recipe.key)
57+
// or square bracket notation (recipe["key"]). Dot notation is more common.
58+
console.log("Dish Name: " + recipe.name);
59+
console.log("Serves: " + recipe.servings);
60+
61+
// --- Updating data ---
62+
// You can change a value by assigning a new one to its key.
63+
console.log("\nOops, we have more guests! Let's double the recipe.");
64+
recipe.servings = 8;
65+
console.log("Now serves: " + recipe.servings);
66+
67+
// --- Adding new data ---
68+
// You can add a new key-value pair just by assigning it.
69+
recipe.difficulty = "Easy";
70+
console.log("Difficulty: " + recipe.difficulty);
71+
```
72+
73+
<!-- langtabs-end -->
74+
75+
Dictionaries are incredibly versatile. You'll use them constantly to represent anything with a set of properties: a user profile (with keys like `username`, `email`, `id`), a product in an online store (`name`, `price`, `sku`), or configuration settings for an application. They are the go-to tool whenever you need to look up a value by its name instead of its numerical position.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Wait for the Water to Boil (While Loops)
2+
3+
In the last section, we used a `for` loop to go through every item on our recipe card. A `for` loop is perfect when you know exactly how many times you need to repeat something—once for each ingredient in a list, for example.
4+
5+
But what if you don't know how many times you need to repeat? What if you need to repeat an action *until* a certain condition is met?
6+
7+
This is where the **`while` loop** comes in. It's like waiting for a pot of water to boil. You don't say, "I'm going to check the temperature 10 times." You say, "**While** the water is not yet boiling, I will keep waiting."
8+
9+
A `while` loop continuously executes a block of code as long as a given condition remains `true`.
10+
11+
### The Danger of a Never-Ending Boil
12+
13+
A `while` loop needs one crucial thing: something inside the loop must eventually change the condition to `false`. If not, the loop will run forever, creating an **infinite loop**.
14+
15+
```admonish warning title="Infinite Loop Warning!"
16+
An infinite loop is a common bug where a loop's condition never becomes `false`. This will cause your program to get stuck and become unresponsive. It’s like a chef who turns on a mixer and forgets to turn it off—it will just keep running until you pull the plug! Always make sure your loop is making progress toward its end condition.
17+
```
18+
19+
Let's simulate our boiling water example. We'll start with a low temperature and keep "heating" it (increasing the value) inside the loop until it reaches the boiling point.
20+
21+
<!-- langtabs-start -->
22+
23+
```python
24+
water_temp = 20 # The starting temperature in Celsius
25+
26+
print("Putting the pot on the stove...")
27+
28+
# The loop will continue as long as this condition is True.
29+
while water_temp < 100:
30+
print(f"Water is at {water_temp}°C. Still simmering...")
31+
# This is the crucial part: we change the value we are checking.
32+
# Each time the loop runs, the temperature gets closer to 100.
33+
water_temp += 10
34+
35+
# Once water_temp is 100 or more, the condition becomes False,
36+
# and the loop stops. This line runs next.
37+
print(f"Ding! The water is at {water_temp}°C. Time to add the pasta!")
38+
```
39+
40+
```javascript
41+
let waterTemp = 20; // The starting temperature in Celsius
42+
43+
console.log("Putting the pot on the stove...");
44+
45+
// The loop will continue as long as this condition is true.
46+
while (waterTemp < 100) {
47+
console.log(`Water is at ${waterTemp}°C. Still simmering...`);
48+
// This is the crucial part: we change the value we are checking.
49+
// Each time the loop runs, the temperature gets closer to 100.
50+
waterTemp += 10;
51+
}
52+
53+
// Once waterTemp is 100 or more, the condition becomes false,
54+
// and the loop stops. This line runs next.
55+
console.log(`Ding! The water is at ${waterTemp}°C. Time to add the pasta!`);
56+
```
57+
58+
<!-- langtabs-end -->
59+
60+
The `while` loop is essential for situations where the number of iterations isn't known beforehand, like waiting for a user to type "quit", processing data until a file ends, or, in our final project, giving a player guesses until they win or run out of tries.

0 commit comments

Comments
 (0)