Functions Flashcards
A Function Expresssion that prints a pizza order
const takeOrder = () => { console.log('Order: pizza'); };
takeOrder();
What are parameters?
Parameters are variables in a function definition that represent data we can input into the function.
Function Declaration example syntax:
function bar() { return 3; }
bar() //3
bar //function
Anonymous Function Expression example syntax:
//anonymous function expression var a = function() { return 3; }
//named function expression var a = function bar() { return 3; }
//self invoking function expression (function sayHello() { alert("hello!"); })();
Refactor: const multiplyByNineFifths = (celsius) => { return celsius * (9/5); };
const multiplyByNineFifths = celsius => celsius * (9/5);
Review Functions
Functions are written to perform a task.
Functions take data, perform a set of tasks on the data, and then return the result.
We can define parameters to be used when calling the function.
When calling a function, we can pass in arguments, which will set the function’s parameters.
We can use return to return the result of a function which allows us to call functions anywhere, even inside other functions.
Which of the following demonstrates correct syntax for a function?
Which of the following demonstrates correct syntax for a function?
myFunction = () {
}
const myFunction = () => {
}
const myFunction{ () } (
)
const myFunction = {
}
const myFunction = () => {
}
What’s the purpose of a parameter?
What’s the purpose of a parameter?
To make the data the function uses visible.
To prevent the function from being called without certain variables defined.
To pass data to a function, which are then used when calling the function.
To pass data to a function, which are then used when calling the function.
Which of the following is a parameter in the block of code below?
let input = prompt(‘Enter input value’);
const controlVal = input / 2 + 3;
const multiplier = (number, phase) => {
const val = number * controlVal + phase;
console.log(val);
};
Which of the following is a parameter in the block of code below?
input
number
controlVal
val
number
Which of the following is the correct way to call the function below?
const multiplier = (number) => { console.log(3 * number); }; Which of the following is the correct way to call the function below?
multiplier(5)
multiplier{5}
multiplier[5]
multiplier 5
multiplier(5)
What will this code print to the console?
const sleepTimer = (alarm) => { console.log('My alarm is set for: ' + alarm); }
sleepTimer(‘8:30AM’);
What will this code print to the console?
My alarm is set for: undefined
My alarm is set for: sleepTimer
My alarm is set for: alarm
My alarm is set for: 8:30AM
My alarm is set for: 8:30AM
What will this code print to the console?
const workoutJournal = (miles, avgTime) => { console.log('I ran ' + miles + ' miles at an average of ' + avgTime + ' per mile.'); }
workoutJournal(‘3’);
What will this code print to the console?
I ran 3 miles at an average of undefined per mile.
Uncaught ReferenceError: avgTime is not defined.
I ran 3 miles at an average of per mile.
I ran 3 miles at an average of 7 minutes per mile.
I ran 3 miles at an average of undefined per mile.
Which of the following best describes what a function in JavaScript is used for?
Which of the following best describes what a function in JavaScript is used for?
A function is a reusable piece of code that takes an input, operates on it, then returns an output.
A function is used for math formulas.
A function allows for the use of mathematical operators.
A function creates new variables.
A function is a reusable piece of code that takes an input, operates on it, then returns an output.
How can you call this function?
const getCurrentDate = () => { let date = new Date(); return date; } How can you call this function?
getCurrentDate();
const getCurrentDate();
getCurrentDate(){}
getCurrentDate(todaysDate);
getCurrentDate();
Which of the following most accurately describes the volumeOfCube() function?
let side = 5;
function volumeOfCube () { side * side * side; }
volumeCube(); // Output: 125. Which of the following most accurately describes the `volumeOfCube()` function?
It is a function declaration.
It is an arrow function declaration.
It is an arrow function.
It is a function expression.
It is a function declaration.