Eloquent JavaScript Exercises Flashcards

1
Q
Write program to output:
#
##
###
####
#####
######
#######
A
for (let hash = '#'; hash.length <= 7; hash += '#') {
	console.log(hash);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you define and invoke a function? Use simple syntax.

A
function name(parameter1, parameter2, parameter3) {
    code to be executed
}
ex:
function square(x, x)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are parameters within a function?

A

Parameters act as placeholders for values within a function. These values will be filled when the function is invoked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is a and b in the below? What do the ( ) that surround a and b represent? 
function myFunction(a , b){
console.log(a + b);
}
A

a and b are the parameters of the function.

the parenthesis surrounding a and b are “arguments”. This function has arguments.

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

Call the processArg function with an argument of 7 and assign its return value to the variable processed.

var processed = 0;

function processArg(num) {
  return (num + 3) / 5;
}
A

processed = processArg(7);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What does this program do?
// Setup
var lastName = "Lovelace";
// Only change code below this line.
var lastLetterOfLastName = lastName[lastName.length - 1];
A

It sets the lastLetterOfLastName variable to the last letter in the lastName variable’s string. In this case it would be the letter “e”.

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

What is a nested array called?

A

Multi-dimensional Array

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

How is data accessed inside of an array? What encapsulates this data?

A

Accessed by indexes. Brackets [ ] are what encapsulate the indexes of an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
var arr = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
    [[10,11,12], 13, 14]
];

//equals????
arr[3];
arr[3][0];
arr[3][0][1];

A

arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11

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

What does the function .push() do?

What is arr now?
var arr = [1,2,3];
arr.push(4);

A

.push() takes one or more parameters and “pushes” them onto the end of the array.

// arr is now [1,2,3,4]

Remember that .push() ADDS another index to the end of the array.

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

What does .pop() do and what is the variable myArray = and removedFromMyArray = now?

// Setup
var myArray = [["John", 23], ["cat", 2]];
// Only change code below this line.
var removedFromMyArray = myArray.pop();
A

.pop() literally pops off the last index in an array and assigns it to a new variable.

So, myArray now equals [“John”, 23]; and removedFromMyArray equals [“cat”, 2];

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

What does the function .shift() do?

What does ourArray equal now?
var ourArray = ["Stimpson", "J", ["cat"]];
removedFromOurArray = ourArray.shift();
A

.shift() is similar to .pop() except that is removes the first index item in the array and assigns it to a new variable.

So, ourArray now equals [“J”,[“cat”]]; and removedFromOurArray equals [“Stimpson”];

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

What does the function .unshift() do?

What do the variables below equal now?

var ourArray = [“Stimpson”, “J”, “cat”];
ourArray.shift();
ourArray.unshift(“Happy”);

A

.unshift() works exactly like .push(), but instead of adding the element at the end of the array, unshift() adds the element at the beginning of the array.

var ourArray = [“Stimpson”, “J”, “cat”];
ourArray.shift(); // ourArray now equals [“J”, “cat”]
ourArray.unshift(“Happy”);
// ourArray now equals [“Happy”, “J”, “cat”]

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