Functions Flashcards

1
Q

Create a function named ‘arrayCounter’ that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a ‘string’, ‘number’ or ‘undefined’ value is passed in.

A
function arrayCounter(array) {
  if (typeof array === "string" || typeof array === "number" || typeof array === "undefined"){
    return 0;
  } else {
    return array.length;
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
var x = 1;
var y = 1;
function hypotenuse(a , b) {
  var cSquared = a * a + b * b;
  x = Math.sqrt(cSquared);
  return x;
}

hypotenuse(x, y);

When the ‘hypotenuse’ function is called the value of ‘x’ changes from 1. Fix it so that ‘x’ is still 1 in the global scope.

A
var x = 1;
var y = 1;
function hypotenuse(a , b) {
  var cSquared = a * a + b * b;
  var x = Math.sqrt(cSquared);
  return x;
}

hypotenuse(x, y);

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

var anonymousFunction;

Set the variable ‘anonymousFunction’ to be an anonymous function without any code to be executed.

A

var anonymousFunction = function(){};

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

(function () {
window.didExecute = true;
});

Make the anonymous function with the code ‘window.didExecute = true’ execute.

A

(function () {
window.didExecute = true;
})();

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

Aside from providing the return value to the code that called the function, what other behavior does return have that is useful?

A

it stops execution of the function immediately

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

In the following code, what will be output by the console.log call?

 function example ( ) {
  var color = "red";
  return color;
}

example(“blue”);

console.log(color);

A

An error is thrown, color is not defined

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

Given the following code snippet, what are the values of a and b inside of the function when it is called?

 function example (a, b) {
  console.log("a === ", a,  " , b === ", b);
}

example(“Treehouse”);

A

a === “Treehouse”, b === undefined

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

When the return statement is called without a value, what is the value returned from the function?

A

undefined

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

Why are functions so important, especially in the browser?

A

it allows you to attach code to be executed when events like clicks and key-presses happen

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

What is an anonymous function?

A

it is a function declared without a name that can be stored in a variable, passed as a value, or invoked immediately

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

In the following code, what value will be printed to the console?

var animal = “cow”;

function display (animal) {
  console.log(animal)
}

animal = “pig”

display(“horse”);

A

“horse”

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

What happens when a function is called with fewer arguments than are defined in the function?

A

any remaining arguments hold the value undefined

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

Why is it a common pattern to execute anonymous functions immediately in your program?

A

functions provide a new variable scope, so variables can be used privately and without collisions

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