Functions in Node (Summary)

When this course began, a function was something you filled in, with its first line and closing brace supplied. Now you can write the whole thing yourself, from the function line to the closing brace, and build a small program out of several functions that work together. Your arcade score toolkit is the proof. It turns hits and misses into a line like Ada: 130 points (Pro), and you could change its scoring rule without touching the message code.

What You Can Do Now

These are the skills you practiced, in the order you met them:

  • Write and call functions. You turned repeated lines into a function declaration with parameters, then called it with different arguments. You learned that a function’s code runs only when you call it, and that arguments match parameters by position, not by name.
  • Return a value instead of only logging it. You met the two exits from the video. console.log() shows a value to you, while return hands it back to your code, ends the function, and lets the returned value take the call’s place. A function that never reaches a return gives back undefined.
  • Run your functions locally. You moved a function from an exercise into a file in scratch/, left out the exercise’s last line, export { countTokens, canPlay, prizeFor };, because that export line was only the platform wiring, and called it with values of your own.
  • Place names in the right box. Every block and function body keeps its own names, a parameter is a new local variable, and a lookup moves outward but never into an inner or neighboring box. You fixed each ReferenceError by declaring a name in the smallest box that holds every line that needs it.
  • Split a program into functions. You planned a brief as comments, wrote a contract for each function, wired them up with stubs, and checked each one against hand-worked values, including the boundaries. The calculating functions returned numbers, and only the outer edge of the file logged anything.

If any of these still feel shaky, go back to the quiz you just took and reread the lesson behind a question you missed. The mentor can also ask you a few fresh questions, one at a time, and explain any answer you get wrong.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.