Learn To Code Part IV: Understanding Functions And Making A Guessing Game

Learn To Code Part IV: Understanding Functions And Making A Guessing Game

If you’ve made it this far in our programming lessons, you’ve arrived at the reward. Today we’re learning about functions and then we’re going to make a very simple guessing game.

These lessons work best with the video, which you can see above, but we’re also providing text for reference below. Even if you do prefer to read, the videos will be more explicit and demonstrate how to do everything we’re discussing. If the text seems a bit too complicated, be sure to watch the video.

Today is our last-ish lesson (there will be an “epilogue” with a mini-lesson on best practices and additional resources following this one), and we’re going to cover two things: functions and making a very simple guessing game. If you made it through Lesson 3, chances are you’ll find functions pretty easy to learn. We’re going to tackle that first and then use that knowledge—plus a little HTML—to put the game together.

What The F Is A Function?

Oh, come on, you already know all about functions. You’ve been using one this entire time: alert(). A function is basically a means of calling a large chunk of code with just a tiny bit of text. Functions are kind of like the text expansion of programming. While you’ve used alert(), that’s just a built-in function (of which there are many in JavaScript and all modern object-oriented programming languages). Today, we’re going to create our own function that will eventually help us make a guessing game. First, let’s take a look at how a function is declared in your code:

function nameOfFunction(variables)