Lesson 13 ยท JavaScript Deep Dive ยท about 20 minutes

๐Ÿช„ Functions: Your Own Spells

So far you've been casting OTHER people's spells. Today you learn to write your OWN: a chunk of code with a name, that you write once and cast a thousand times. This is the biggest power-up in all of programming.

๐ŸŽฌ Watch first (90 seconds)

Write the spell once, cast it forever

Imagine you invent an amazing recipe for robot cookies. Do you re-invent it every time you're hungry? No way โ€” you write it down once, give it a name ("Robot Cookies"), and then just say the name whenever you want cookies.

A function is exactly that: a recipe with a name. Here's one:

function cheer(name) {
  return "Go " + name + "!";
}

Read it like a wizard would:

And here's how you CAST it โ€” this is called calling the function:

cheer("Robo Sneakers")   // โžก๏ธ gives back "Go Robo Sneakers!"
cheer("Tugay")           // โžก๏ธ gives back "Go Tugay!"

Same recipe, different ingredients, different cookies. One spell, endless cheers!

๐Ÿ’ก The one idea of this lesson Functions are two-step magic, and mixing up the steps is the #1 beginner trap:
  1. DEFINING the function (function cheer(name) { ... }) does NOTHING yet. You just wrote the recipe in the cookbook. The kitchen is silent. No cookies exist.
  2. CALLING the function (cheer("Robo Sneakers")) is what actually makes it run. NOW you're cooking.
Writing a recipe is not the same as cooking dinner. If your function "isn't working", ask first: did I ever actually CALL it?

One more secret: you've been calling functions since lesson 8! document.getElementById("buy")? That's a function call โ€” getElementById is a spell somebody at the browser factory wrote for you, "buy" is the ingredient you pour in, and the brick it hands back is its return. You've been a spell-caster all along. Today you become a spell-writer.

๐Ÿ› ๏ธ Build it (the fun part)

๐Ÿงช Your mission Give your shop a price calculator with a silly 100% kid-tax (every price doubles โ€” sorry, grown-ups!) and an announcer spell that writes messages on the page. Then wire the Buy button so one spell casts the other. Spellception!
  1. Open practice/myshop.html in VS Code and find your <script> at the end of the body.
  2. At the TOP of the script, write your first-ever spell (type it, don't copy-paste!):
    function priceWithTax(price) {
      return price * 2;   // 100% kid-tax! ๐Ÿ˜ˆ
    }
    Save and refresh. Notice: nothing happens. That's correct! You only wrote the recipe โ€” nobody cooked it yet.
  3. Now write a second spell right below it. This one doesn't return anything โ€” it DOES something instead:
    function announce(text) {
      document.getElementById("message").textContent = text;
    }
    (You need your <p id="message"></p> from lesson 8 โ€” add it back if it's gone.)
  4. Time to COOK. Change your Buy button's click listener so the inside becomes:
    btn.addEventListener("click", function () {
      announce("Total: " + priceWithTax(5) + " coins");
    });
    Look closely โ€” that's a spell calling a spell: priceWithTax(5) runs first and hands back 10, then announce writes the whole sentence on the page.
  5. Save (โŒ˜S), refresh (โŒ˜R), click Buy now. Does it say Total: 10 coins? You just wrote, cast, and CHAINED your own spells. ๐Ÿช„๐ŸŽ‰

๐Ÿ•น๏ธ Try it right here โ€” the lesson checks your code!

Good news: THESE preview boxes have their electricity ON โ€” your scripts really run as you type. Build it, watch it appear, then press Check my code โœ”.

๐Ÿ”จ Now break it!

Do these in your real myshop.html file. Predict what will happen BEFORE you refresh!

  1. Cast it three times. Under your button wiring, add three calls with different ingredients: console.log(priceWithTax(5));, console.log(priceWithTax(50));, console.log(priceWithTax(100));. Refresh and open the Console (right-click โ†’ Inspect โ†’ Console). Three answers from ONE recipe โ€” that's the whole point of functions.
  2. Steal the return. Delete the word return from priceWithTax (keep price * 2;). Refresh, click Buy. It says Total: undefined coins! Meet undefined โ€” JavaScript's way of saying "the box came back empty." Without return, the spell still ran, but it handed nothing back. You will meet this word 1000 more times in your coding life โ€” now you know it just means "empty box". Put return back.
  3. Rename only the recipe. Change the definition to function priceWithTaxx(price) but leave the CALL as priceWithTax(5). Refresh, click Buy, check the Console: a red error โ€” priceWithTax is not defined. The browser looked for a recipe with that exact name and the cookbook page was missing. Fix the name back.
โš ๏ธ The two classic function face-plants If your function "does nothing": you probably never called it โ€” the recipe is in the book but nobody cooked. If your page shows undefined: your function ran but forgot to return โ€” the delivery robot arrived with an empty box. Check those two things first, every time.

๐ŸŽฎ Quiz time

No peeking at the notes above โ€” trying to remember is what makes it stick!

๐Ÿ“š Want more?

โญ Best thing to read next MDN: Functions โ€” reusable blocks of code โ€” the official Mozilla guide to today's magic, with more examples of parameters and return. Reading it counts as a lesson block! And keep the JS Cheat Sheet nearby โ€” add today's spell pattern to it.