JavaScript Functions Worksheet

Question 1

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

Lines of code that can be repreated many times over.
Question 2

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

They are called aurguments
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 all functuions return a value
Question 4

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

They bodu of the function is where the code you want to run is at and the character to enclsoe the function is a squiggly brakcet "{}"
Question 5

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

To run or invoke a function is summed up to running the function all you have to do is type the name of the function and set the paramerters you want after that.
Question 6

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

You would use a comma in between the two+ parameters
Question 7

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


function convertKilometersToMiles(km)
    return km * 0.6217;
}
                

There is no squiggly bracket after the last parathesis of km
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 functions being invoked are prompt and alert. promt is the only function returning a value because it is taking user imput then return whatever they typed in.

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.