JavaScript Functions Worksheet

Question 1

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

Its a reusable block designed to do a certain task and you would want to use it in your code because its able to break doqn complex codes into smaller and manageable to veiww
Question 2

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

Its called arguments
Question 3

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

basically a block that gives the code instructions and and they are enclosed by {}
Question 4

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

It means to tell the computer to run the code or a certain type of code
Question 5

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

You use the comma to seperate them
Question 6

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


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

Its missing theopen curly bracket
Question 7

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 () returns the code and we know this because the code assigns its results to a variable

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.