Your First Function
You just read code β now you'll write it. This is going to be fun. π±
Here's the heart of it: a value you can name and hand back. You wrap that up in a little machine called a function. You give the machine a name, and when someone runs it, it hands a value back to them. Handing a value back is called returning.
Here's that same tiny machine that always returns the number 7:
function lucky() {
return 7;
}
Read it slowly:
functionsays "I'm building a machine."luckyis the machine's name.return 7means "hand back the value 7."
Your task
Write a function called answer that returns the number 42.
(Just fill in what's inside the machine β don't worry about the rest yet.)
β‘ 15 XP
π§± Code blocks
Drag a block into the editor, or tap it to drop it in where your cursor is. Add them in order to build your answer.
solution.jsRuns in your browser
Loading editorβ¦
Tests1 to pass
answer() gives back 42
Run your code to check these.