From the course: Computer Science Principles Lab: JavaScript

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Customize functions with parameters

Customize functions with parameters - JavaScript Tutorial

From the course: Computer Science Principles Lab: JavaScript

Start my 1-month free trial

Customize functions with parameters

- [Instructor] When we have a function, we can accept values to help define how the function works. We have two functions in our code here that we can customize by sending in a value into them to change how they work. Let's start with the init function. When we start our game, we can define what the score can be. Often, it will need to be zero, but in some games, you start the score high and it lowers as you play. Either way, we can send a value to the init function when we call it, and use that value in the function. First we need to create a variable that will be used only within the function, that will hold the value we sent to it. We can call it newScore. We add it inside the parentheses on the first line of the function. Now, instead of using the literal value of 1000, we can use the newScore variable instead. Save your code, and reload in the browser. You'll find that you get an NAN response in the console. That is because we failed to send in a value to initialize the myScore…

Contents