๐ฎ Day 1 - Spell Solutions#
Oda the Data Otterโs Magic Answer Key! ๐ฆฆโจ
โจ Spell 1: Simple If-Else Magic - Solutions#
Activity Questions & Answers:#
1. Change my_favorite_number to different values (try 3, 10, 1)
my_favorite_number <- 3: Output โ โ๐ญ Thatโs a nice small number!โ (because 3 is not > 5)my_favorite_number <- 10: Output โ โ๐ Wow! Thatโs a big number!โ (because 10 > 5)my_favorite_number <- 1: Output โ โ๐ญ Thatโs a nice small number!โ (because 1 is not > 5)
2. Can you predict the message before running it?
If the number is greater than 5 โ โbig numberโ message
If the number is 5 or less โ โsmall numberโ message
โจ Spell 2: Multiple Choices - Solutions#
Activity Questions & Answers:#
1. Change favorite_color to your actual favorite color
If your color is โredโ, โblueโ, or โgreenโ โ specific mood message
Any other color โ โโจ You have a unique and magical taste!โ
2. If your color isnโt there, what message do you get?
You get the default message: โโจ You have a unique and magical taste!โ
3. Extension: Add your favorite color with a special message
# set the locale to English and UTF-8 encoding so that emojis can be displayed correctly
Sys.setlocale("LC_CTYPE", "en_US.UTF-8")
# ๐ Oda's color mood detector with added purple option
favorite_color <- "purple"
if (favorite_color == "red") {
print("๐ฅ You like bold and exciting adventures!")
} else if (favorite_color == "blue") {
print("๐ You love calm and peaceful vibes!")
} else if (favorite_color == "green") {
print("๐ฑ You enjoy nature and growing things!")
} else if (favorite_color == "purple") { # NEW line added to include a new color purple
print("๐ You love magic and mystery!") # NEW line added
} else {
print("โจ You have a unique and magical taste!")
}
[1] "๐ You love magic and mystery!"
โจ Spell 3: Number Range Detective - Solutions#
Activity Questions & Answers:#
1. Predict which otter for each number:
mystery_number <- 5: โ๐ฃ Tiny number - like a baby otter!โ (5 < 10)mystery_number <- 25: โ๐ฆฆ Medium number - like a young otter!โ (25 < 50)mystery_number <- 75: โ๐๏ธ Big number - like a mountain otter!โ (75 < 100)mystery_number <- 150: โ๐ Huge number - like a space otter!โ (150 >= 100)
2. What happens with 0 or negative numbers?
mystery_number <- 0: โ๐ฃ Tiny number - like a baby otter!โ (0 < 10)mystery_number <- -5: โ๐ฃ Tiny number - like a baby otter!โ (-5 < 10)
3. Why check smaller numbers first?
R checks conditions from top to bottom and stops at the first TRUE condition
If we checked larger ranges first, smaller numbers might never reach their specific conditions
โจ Spell 4: AND Condition Magic - Solutions#
Activity Questions & Answers:#
1. Test weather combinations:
temperature <- 15, weather <- "sunny": โ๐ Maybe itโs a good day to stay insideโฆโ (15 is not > 20)temperature <- 25, weather <- "rainy": โ๐ Maybe itโs a good day to stay insideโฆโ (weather is not โsunnyโ)temperature <- 30, weather <- "sunny": โ๐๏ธ Perfect day for swimming and playing!โ (both conditions true)
2. Challenge Questions:
Only temperature good but weather bad: No perfect day (need BOTH conditions)
Only weather good but temperature bad: No perfect day (need BOTH conditions)
Why need BOTH true: AND (&) requires ALL conditions to be true
3. ๐ฏ Experiment: Try creating your own perfect day conditions: Example output:
temperature <- 25
weather <- "sunny"
wind_speed <- 60
if ((temperature > 20) & (weather == "sunny") & (wind_speed < 10)) {
print("๐๏ธ Perfect day for swimming and playing!")
print("๐ฆฆ Oda is super happy!")
} else if (wind_speed > 50){
print("๐จ Wow I'm being blown away, hide in the bunk!")
}else {
print("๐ Maybe it's a good day to stay inside and code!")
}
[1] "๐จ Wow I'm being blown away, hide in the bunk!"
(Because wind_speed = 60, which is > 50, so the first condition fails and the second condition is true)
โจ Spell 5: OR Condition Magic - Solutions#
Activity Questions & Answers:#
1. Test different snacks:
snack <- "apple": โ๐ฅฐ Yummy! Oda loves healthy snacks!โ (apple is in healthy list)snack <- "banana": โ๐ฅฐ Yummy! Oda loves healthy snacks!โ (banana is in healthy list)snack <- "cookie": โ๐ช Sweet treat! But just a little bit!โ (cookie is in sweet list)snack <- "pizza": โ๐ค Hmm, Oda isnโt sure about this snackโฆโ (not in any list)
2. What happens with โAppleโ vs โappleโ?
snack <- "Apple": โ๐ค Hmm, Oda isnโt sureโฆโ (R is case-sensitive - โAppleโ โ โappleโ)
3. Extension: Add more snacks
# ๐ Oda's snack time detector with added protein category
snack <- "cheese"
if (snack == "apple" | snack == "banana" | snack == "carrot") {
print("๐ฅฐ Yummy! Oda loves healthy snacks!")
print("๐ช This will give me energy for coding!")
} else if (snack == "cookie" | snack == "candy") {
print("๐ช Sweet treat! But just a little bit!")
} else if (snack == "cheese" | snack == "nuts") {
print("๐ง Protein power! Great for brain fuel!")
} else {
print("๐ค Hmm, Oda isn't sure about this snack...")
}
[1] "๐ง Protein power! Great for brain fuel!"
โจ Spell 6: Age Group Sorter - Solutions#
Activity Questions & Answers:#
1. Test different ages:
age <- 5: โ๐ฃ Little explorer - just starting the adventure!โ (5 < 6)age <- 8: โ๐งธ Young adventurer - ready for fun!โ (6 โค 8 โค 9)age <- 12: โ๐งโโ๏ธ Data wizard in training - thatโs you!โ (10 โค 12 โค 14)age <- 16: โ๐ Teen tech master - almost ready to rule the world!โ (15 โค 16 โค 18)age <- 25: โ๐ Wise adult - teaching the next generation!โ (25 > 18)
2. Edge cases:
age <- 6: โ๐งธ Young adventurerโ (6 โฅ 6 is true)age <- 14: โ๐งโโ๏ธ Data wizard in trainingโ (14 โค 14 is true)
3. Why use >= and <=?
To include the boundary ages (like exactly 6 or exactly 14)
Makes sure no ages โfall through the cracksโ
4. ๐ฏ Challenge Solution: Age-Based Activity Sorter
# ๐ฏ Oda's Age-Based Activity Sorter - Perfect activities for each age group!
age <- 12 # Try different ages: 4, 7, 13, 16, 25
if (age < 6) {
print("๐ฃ Little Explorer Activities:")
print("๐งธ Play with building blocks and stuffed animals")
print("๐ Listen to picture book stories")
print("๐จ Finger painting and simple crafts")
print("๐ต Sing-along songs and nursery rhymes")
} else if (age >= 6 & age <= 9) {
print("๐งธ Young Adventurer Activities:")
print("๐ฎ Simple board games and puzzles")
print("โฝ Team sports like soccer or basketball")
print("๐ฌ Basic science experiments")
print("๐ญ Drama and pretend play")
} else if (age >= 10 & age <= 14) {
print("๐งโโ๏ธ Data Wizard Activities:")
print("๐ป Learn coding and computer programming!")
print("๐ธ Learn musical instruments")
print("๐ฑ Create digital art and videos")
print("๐งฉ Complex strategy games and escape rooms")
} else if (age >= 15 & age <= 18) {
print("๐ Teen Tech Master Activities:")
print("๐ Learn to drive and gain independence")
print("๐ผ Part-time jobs and internships")
print("๐ Prepare for college and career planning")
print("๐ Volunteer work and community service")
} else {
print("๐ Wise Adult Activities:")
print("๐จโ๐ซ Teach and mentor the next generation")
print("๐ฐ Career advancement and financial planning")
print("๐ฟ Hobbies like gardening, cooking, or travel")
}
print(paste("๐ Perfect activities for someone who is", age, "years old!"))
[1] "๐งโโ๏ธ Data Wizard Activities:"
[1] "๐ป Learn coding and computer programming!"
[1] "๐ธ Learn musical instruments"
[1] "๐ฑ Create digital art and videos"
[1] "๐งฉ Complex strategy games and escape rooms"
[1] "๐ Perfect activities for someone who is 12 years old!"
โจ Spell 7: Multiple Actions Spell - Solutions#
Activity Questions & Answers:#
1. Test magic words:
magic_word <- "abracadabra": Full classic magic show (4 actions)magic_word <- "alakazam": Fireworks show (3 actions)magic_word <- "hocus pocus": Unknown magic word message (2 actions)
2. Why multiple print() statements?
Each print() creates a separate line of output
Creates a sequence of actions that tell a story
Makes the magic show feel more dramatic and realistic
3. Creative Challenge: Add third magic word
# ๐ช Oda's magic show with multiple tricks including new "presto" magic
magic_word <- "presto"
if (magic_word == "abracadabra") {
print("โจ *Sparkles appear in the air*")
print("๐ฉ *A rabbit pops out of the hat*")
print("๐ *The crowd cheers loudly*")
print("๐ฆฆ Oda takes a bow!")
} else if (magic_word == "alakazam") {
print("๐ฅ *Thunder sound effect*")
print("๐ *Colorful fireworks explode*")
print("๐ฆฆ Oda does a backflip!")
} else if (magic_word == "presto") {
print("๐ฐ *Magic rabbits hop everywhere*")
print("๐ *Rainbow appears in the sky*")
print("๐ฆฆ Oda waves her magic wand!")
} else {
print("๐
Oops! That's not a magic word Oda knows...")
print("๐ก Try 'abracadabra', 'alakazam', or 'presto'!")
}
[1] "๐ฐ *Magic rabbits hop everywhere*"
[1] "๐ *Rainbow appears in the sky*"
[1] "๐ฆฆ Oda waves her magic wand!"
โจ Spell 8: No Else Challenge - Solutions#
Activity Questions & Answers:#
1. Test point values:
points <- 30: Only โ๐ฏ You have 30 points total!โ (no achievements met)points <- 60: โ๐ GOOD JOB! Keep practicing!โ + total points (โฅ50 condition met)points <- 80: โโญ AWESOME! Youโre doing great!โ + โ๐ GOOD JOB! Keep practicing!โ + total points (โฅ75 and โฅ50 met)points <- 120: All three achievements + total points (โฅ100, โฅ75, โฅ50 all met)
2. Why multiple achievement messages?
Each
ifstatement runs independently (noelseto stop them)High scores qualify for multiple achievement levels simultaneously
Like earning multiple badges in a game
3. Minimum points for all achievements?
Need at least 100 points to trigger all three achievement conditions
โจ Spell 9: Code Order Magic - Solutions#
Activity Questions & Answers:#
1. Compare both versions:
Version 9A (Correct):
position <- 1: โ๐ฅ GOLD MEDAL!โposition <- 2: โ๐ฅ SILVER MEDAL!โposition <- 3: โ๐ฅ BRONZE MEDAL!โ
Version 9B (Wrong):
position <- 1: โ๐ Good race! Keep practicing!โ (WRONG!)position <- 2: โ๐ Good race! Keep practicing!โ (WRONG!)position <- 3: โ๐ Good race! Keep practicing!โ (WRONG!)
2. Why Version 9B always says โGood race!โ
First condition
position >= 1is TRUE for positions 1, 2, and 3R stops at the first TRUE condition and never checks the others
3. Which conditions never run in Version 2?
The
position == 1andposition == 2conditions never runBecause
position >= 1catches everything first
Key Lesson: Always check SPECIFIC conditions before GENERAL ones!
โจ Spell 10: String Detective - Solutions#
Activity Questions & Answers:#
1. Test different names:
name <- "Jo": โ๐ You have a nice short name!โ (2 letters โค 4)name <- "Elizabeth": โ๐ Wow! You have a beautifully long name!โ (9 letters โฅ 8)name <- "Sam": โ๐ You have a nice short name!โ (3 letters โค 4)name <- "Christopher": โ๐ Wow! You have a beautifully long name!โ (11 letters โฅ 8)
2. What does nchar() do?
nchar()counts the number of characters (letters) in a text stringnchar("hello")returns 5nchar("R")returns 1
3. Why canโt Oda find Mike? THE BIG PROBLEM!
When
name <- "Mike":First condition:
nchar("Mike") <= 4โ4 <= 4โ TRUE!R prints โ๐ You have a nice short name!โ and stops
The Mike-finding condition never gets checked!
4. How to fix it:
# ๐ FIXED version - Oda can now find Mike! Check for Mike FIRST:
name <- "Mike"
if (name == "mike" | name == "Mike" | name == "Michael" | name == "michael"){
print("๐ฅ Hi Mike, long time no see! I've caught some clams today! ๐ ๐ ๐ฆช")
} else if (nchar(name) <= 4){
print("๐ You have a nice short name!")
} else if (nchar(name) >= 8) {
print("๐ Wow! You have a beautifully long name!")
} else {
print("โจ You have a perfectly medium-sized name!")
}
# Bonus: Count the letters in your name!
print(paste("๐ข Your name has", nchar(name), "letters!"))
[1] "๐ฅ Hi Mike, long time no see! I've caught some clams today! ๐ ๐ ๐ฆช"
[1] "๐ข Your name has 4 letters!"
(Now Oda can find Mike because we check for his name BEFORE checking the length!)
โจ Spell 11: Temperature Zone Detective - Solutions#
Activity Questions & Answers:#
1. Test temperatures:
temperature <- -5: โ๐ง FREEZING!โ + winter coat message (-5 โค 0)temperature <- 5: โ๐ฅถ COLD! Perfect sweater weather!โ (1 โค 5 โค 10)temperature <- 15: โ๐ COOL! Great for a light jacket!โ (11 โค 15 โค 20)temperature <- 25: โโ๏ธ WARM! Perfect for playing outside!โ (21 โค 25 โค 30)temperature <- 35: โ๐ฅ HOT!โ + swimming message (35 โฅ 31)
2. Why use >= and <= instead of > and <?
To include exact boundary temperatures (like exactly 0ยฐ, 21ยฐ, etc.)
Prevents temperatures from โfalling betweenโ categories
temperature <- 20with< 20would not be caught by the โcoolโ category
3. Edge cases:
temperature <- 0: โ๐ง FREEZING!โ (0 โค 0 is true)temperature <- 21: โโ๏ธ WARM!โ (21 โฅ 21 is true)temperature <- 20.5: โ๐ค Something seems fishy with this temperatureโฆโ
โจ Spell 12: Magical Creatures - Solutions#
Activity Questions & Answers:#
1. Test different combinations:
creature_size <- 15, creature_color <- "purple": โ๐งโโ๏ธ You found a tiny fairy!โcreature_size <- 50, creature_color <- "gold": โ๐ You found a baby dragon!โcreature_size <- 50, creature_color <- "blue": โ๐ฆฆ You found Oda the Otter!โcreature_size <- 150, creature_color <- "any": โ๐น You found a friendly giant!โ
2. What happens with negative/zero size?
creature_size <- 0: โ๐ You found a magical bug!โ (0 โค 20, not purple)creature_size <- -5: โ๐ You found a magical bug!โ (-5 โค 20, not purple)
3. How nested conditions work:
Outer condition checks size range first
Inner conditions check color within that size range
Like a decision tree: size โ then color โ then result
4. Extension ideas:
# ๐ฆ EXTENDED Magical Creature Classifier with more sizes and colors
creature_size <- 150 # Size in cm
creature_color <- "silver" # Try different colors
if (creature_size <= 20) {
if (creature_color == "purple") {
print("๐งโโ๏ธ You found a tiny fairy!")
} else if (creature_color == "invisible") {
print("๐ป You found an invisible sprite!")
} else {
print("๐ You found a magical bug!")
}
} else if (creature_size >= 21 & creature_size <= 100) {
if (creature_color == "purple") {
print("๐ฆ You found a unicorn!")
} else if (creature_color == "gold") {
print("๐ You found a baby dragon!")
} else if (creature_color == "rainbow") {
print("๐ฆ You found a rainbow unicorn!")
} else {
print("๐ฆฆ You found Oda the Otter!")
}
} else if (creature_size >= 101 & creature_size <= 200) {
if (creature_color == "silver") {
print("๐บ You found a silver wolf!")
} else if (creature_color == "gold") {
print("๐ You found a teenage dragon!")
} else {
print("๐ป You found a friendly bear!")
}
} else if (creature_size >= 201) {
print("๐น You found a friendly giant!")
}
print(paste("๐ Your creature is", creature_size, "cm tall and", creature_color, "colored!"))
[1] "๐บ You found a silver wolf!"
[1] "๐ Your creature is 150 cm tall and silver colored!"
(Because creature_size = 150 falls in the 101-200 range, and creature_color = โsilverโ matches the wolf condition)
5. SUPER CHALLENGE: Flying Ability Extension
# ๐ ULTIMATE Magical Creature Classifier with Flying Abilities!
creature_size <- 50 # Size in cm
creature_color <- "gold" # Try different colors
creature_ability <- "flying" # Try: "flying", "swimming", "invisible"
if (creature_size <= 20) {
if (creature_color == "purple") {
if (creature_ability == "flying") {
print("๐งโโ๏ธโ๏ธ You found a flying tiny fairy!")
print("๐ซ She can sprinkle magic dust from the sky!")
} else if (creature_ability == "invisible") {
print("๐งโโ๏ธ๐ป You found an invisible tiny fairy!")
print("๐ You can only see sparkles where she flies!")
} else {
print("๐งโโ๏ธ You found a tiny fairy!")
}
} else {
print("๐ You found a magical bug!")
}
} else if (creature_size >= 21 & creature_size <= 100) {
if (creature_color == "gold") {
if (creature_ability == "flying") {
print("๐โ๏ธ You found a flying baby dragon!")
print("๐ฅ It breathes fire while soaring through the clouds!")
} else if (creature_ability == "swimming") {
print("๐๐ You found a swimming baby dragon!")
print("๐ง It can breathe underwater and shoots water blasts!")
} else {
print("๐ You found a baby dragon!")
}
} else if (creature_color == "purple") {
if (creature_ability == "flying") {
print("๐ฆโ๏ธ You found a flying unicorn!")
print("๐ It leaves rainbow trails in the sky!")
} else {
print("๐ฆ You found a unicorn!")
}
} else {
print("๐ฆฆ You found Oda the Otter!")
}
} else if (creature_size >= 101) {
if (creature_ability == "flying") {
print("๐นโ๏ธ You found a flying friendly giant!")
print("โ๏ธ The ground shakes when it lands!")
} else {
print("๐น You found a friendly giant!")
}
}
print(paste("๐ Your creature is", creature_size, "cm tall,", creature_color, "colored, and has", creature_ability, "ability!"))
[1] "๐โ๏ธ You found a flying baby dragon!"
[1] "๐ฅ It breathes fire while soaring through the clouds!"
[1] "๐ Your creature is 50 cm tall, gold colored, and has flying ability!"
(Because creature_size = 50, creature_color = โgoldโ, and creature_ability = โflyingโ all match the flying baby dragon conditions)
๐ Congratulations! Youโve mastered all 12 magical spells! Youโre now ready to cast decision-making magic in any R adventure! ๐ฆฆโจ