JavaScript Functions Worksheet

Question 1

What is a function, and why would you ever want to use one in your code?

A function is a series of code that preforms an action that we want to be able to call on. While we can copy and paste the code as needed this would make the code as a whole be long. We can shorten it by creating a function and calling the function when we want the action to take place. In short, it allows us to execute code when needed without having to put those lines repeatly in our code.
Question 2

What do you call the values that get passed into a function?

Parameters are what allows us to pass information into functions.
Question 3

Do all functions return a value? (Note: this is a bit of a trick question - make sure to ask me about this in class.)

Yes and No, it depends on the type of function and how we bulit it.
Question 4

What is the 'body' of a function, and what are the characters that enclose the body of a function?

A 'body' is a group of statements that are enclose in curly bracers'{}'
Question 5

What does it mean to 'call', or 'invoke' a function (note that 'calling' and 'invoking' a function mean the same thing)?

To 'call' or 'invoke' a function means to excute the code for that function.
Question 6

If a function has more than one parameter, what character do you use to separate those parameters?

They must be seperated with a comma ','.
Question 7

What is the problem with this code (explain the syntax error)?


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

The function above doesn't have the first curly bracer after the (km). If we had that there we would be able to set a variable for it and then display it in a console log(as long as we set how many kilometers we wanted to convert).
Question 8

In the code below, there are two functions being invoked. Which one returns a value? Explain why you know this.


const name = prompt("Enter your name");
alert("Hello " + name + "!");
                

The two function being invoked are 'prompt' and 'alert'; 'prompt' takes user input and stores it as a string value. Then 'alert' will take 'name' since it was declared as a variable then return it to display "Hello [user input] !". Using this information I beleive that 'prompt' returns the value of the user's input and stores it in the delcared vairable.

Coding Problems

Coding Problems - See the 'script' tag below this h3 tag. You will have to write some JavaScript code in it.

Always test your work! Check the console log to make sure there are no errors.