Functions Module #18 Flashcards

1
Q

How are multiple values from passed parameters returned?

A

To return multiple values, you can return an object, or an array, like this:

function getData( ) {
  return ["Flavio", 37]
}

let [name, age] = getData( )

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

How are arrow functions different from “regular” functions?

A
function getData() {
  //...
}

to

() => {
  //...
}

But.. notice that we don’t have a name here.

Arrow functions are anonymous. We must assign them to a variable.

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

How are “regular” functions assigned to variables?

A

When we do so, we can remove the name from the function:

let getData = function( ) {
  //...
}

and invoke the function using the variable name:

let getData = function( ) {
  //...
}
getData()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are arrow functions assigned to variables?

A
let getData = ( ) => {
  //...
}
getData( )

If the function body contains just a single statement, you can omit the curly braces and write all on a single line:

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

How is data returned in an arrow function?

A

Arrow functions allow you to have an implicit return: values are returned without having to use the return keyword.

It works when there is a one-line statement in the function body:

const getData = ( ) => ‘test’

getData( ) //’test’

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

How are default values defined in parameters?

A

Like with regular functions, we can have default values for parameters, in case they are not passed:

const getData = (color = 'black', 
                 age = 2) => {
  //do something
}

and we can only return one value.

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

Review how functions get nested inside each other and why.

A

This is very useful because we can create encapsulated code that is limited in its scope by the outer function it’s defined in.

We could have 2 functions define a function with the same name, inside them:

const bark = ( ) => {
  const dosomethingelse = ( ) => {
    //some code here
  }
  dosomethingelse( )
  return "test"
}
const sleep = ( ) => {
  const dosomethingelse = ( ) => {
    //some code here
  }
  dosomethingelse( )
  return "test"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Define recursion.

A
A function that is "invoked" within itself.
Example: 
function doSomething( ){
    console.log('I was called.")
    doSomething( )
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you write a factorial function

A
function factorial(n) {
  return n >= 1 ? n * factorial(n - 1) : 1
}

factorial(1) //1
factorial(2) //2
factorial(3) //6
factorial(4) //24

We can also use an arrow function if we prefer:

const factorial = (n) => {
  return n >= 1 ? n * factorial(n - 1) : 1
}

factorial(1) //1
factorial(2) //2
factorial(3) //6
factorial(4) //24

Finally I know how to do this!!!!!

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

How do you name an arrow function

A

You don’t, arrow functions are anonymous

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

How can you shorten a 1 line arrow function?

A

Eliminate the curly braces.

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