Creating Reusable Code with Functions Flashcards

1
Q

How would you “call a function” named sayHello?

A

Type the name of the function followed by parentheses: sayHello();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What describes a function?

A

A function lets you store a block of code that you can use over and over again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  • 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.
A
function sayHi() {
  alert("Hi");
}

sayHi();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

T/F: A JavaScript function can return several values at once. For example:

function myFunction() {

return ‘Mon’, ‘Tues’, ‘Wed’, ‘Thurs’, ‘Fri’;

}

var days = myFunction();

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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();

A

“Monday”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What JavaScript keyword is used to send back a value from a function?

A

return

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T/F: You can have more than one return statement in a function.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the JavaScript return statement do?

A

return exits a function and sends a value back to the spot in the program where the function was called

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  • 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.
A
function getYear() {
  var year = new Date().getFullYear();
  return year;
}

var yearToday = getYear();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When talking about JavaScript functions, what is an “argument”?

A

A value that you pass to a function when you call the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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;

} ;

_____;

A

getRandom(12);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When talking about JavaScript functions, what is a “parameter”?

A

A variable in which the function stores information passed to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

T/F: You can pass as many arguments to a function as you’d like.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  • 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.
A
function returnValue(arg) {
  return arg;
}

var echo = returnValue(“My argument”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • 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.
A
function max(num1, num2) {
  if (num1 \> num2) {
    return num1;
  } else {
    return num2;
  }
}

alert(max(1, 2));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the code below, what appears in the alert dialogue when this program runs?

var message = “Welcome!”;

function setMessage() {

message = “Go away!”;

}

setMessage();

alert(message);

A

“Go away!”

17
Q

Given the code below, what appears in the alert dialogue when this program runs?

var name = “Trish”;

function setName() {

var name = “Sarah”;

}

setName();

alert(name);

A

“Trish”

18
Q

What is “global scope”?

A

When a variable is accessible anywhere inside a program – in the body of the program and within functions.

19
Q

T/F: When you declare a variable within a function, that variable is only accessible within that function.

A

True

20
Q

Finish the code below so that the variable counter inside the function is only accessible inside that function:

var counter = 0;

function doStuff( ) {

_____ counter = 10;

}

A

var

21
Q

Given this function, write the code to call it:

function warning() {

alert(‘Warning, warning, warning!’);

}

_____;

A

warning()

22
Q

Finish the code below, so that the function returns the message variable:

function greeting( name ) {

var message = “Hello “ + name;

_____;

}

A

return message

23
Q

When you call an function, you can pass that function one or more _____.

A

arguments

24
Q

What is “scope” in JavaScript?

A

Scope is the context in which a variable can be accessed, such as within a function, or within the global scope of the entire program.

25
Q

How do programmers describe the act of running the programming within a function?

A

To “call” a function