Creating Reusable Code with Functions Flashcards
How would you “call a function” named sayHello?
Type the name of the function followed by parentheses: sayHello();
What describes a function?
A function lets you store a block of code that you can use over and over again.
- Create a function named sayHi.
- Inside the sayHi() function add the code to make an alert appear with the word “Hi” in it. The alert command should go inside the function’s code block.
- Call the new sayHi function you just created.
function sayHi() { alert("Hi"); }
sayHi();
T/F: A JavaScript function can return several values at once. For example:
function myFunction() {
return ‘Mon’, ‘Tues’, ‘Wed’, ‘Thurs’, ‘Fri’;
}
var days = myFunction();
False
After the code below runs, what value is stored in the variable dayOfWeek:
function getDay() {
return “Monday”; alert(“Calculating day”);
return “Friday”;
}
var dayOfWeek = getDay();
“Monday”
What JavaScript keyword is used to send back a value from a function?
return
T/F: You can have more than one return statement in a function.
True
What does the JavaScript return statement do?
return exits a function and sends a value back to the spot in the program where the function was called
- Create a function named getYear
- Inside the function’s code block add this line of code
var year = new Date().getFullYear();
This creates a new variable and stores the current year in it. Now, add a statement that returns this variable from the function.
- Call the getYear function: store the returned value of the function in a new variable namedyearToday.
function getYear() { var year = new Date().getFullYear(); return year; }
var yearToday = getYear();
When talking about JavaScript functions, what is an “argument”?
A value that you pass to a function when you call the function.
Complete the code below to call the function and pass the value 12 to it:
function getRandom( upper ) {
return Math.floor(Math.random() * upper) + 1;
} ;
_____;
getRandom(12);
When talking about JavaScript functions, what is a “parameter”?
A variable in which the function stores information passed to it.
T/F: You can pass as many arguments to a function as you’d like.
True
- Here’s a simple challenge: create a function named returnValue() that accepts a single argument, then returns that argument. This isn’t a useful function, but we want to make sure you know how to create a function that can accept information when it is called.
- Now that you’ve created the returnValue function, call it, by passing it a literal string value – a series of characters in quote marks like this: ‘My argument’. Store the results of the function in a variable named echo.
function returnValue(arg) { return arg; }
var echo = returnValue(“My argument”);
- Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You’ll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.
- Call the function and display the results in an alert dialog. You can pass the results of a function directly to the alert() method.
function max(num1, num2) { if (num1 \> num2) { return num1; } else { return num2; } }
alert(max(1, 2));