Eloquent JavaScript Exercises Flashcards
Write program to output: # ## ### #### ##### ###### #######
for (let hash = '#'; hash.length <= 7; hash += '#') { console.log(hash); }
How do you define and invoke a function? Use simple syntax.
function name(parameter1, parameter2, parameter3) { code to be executed } ex: function square(x, x)
What are parameters within a function?
Parameters act as placeholders for values within a function. These values will be filled when the function is invoked.
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 and b are the parameters of the function.
the parenthesis surrounding a and b are “arguments”. This function has arguments.
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; }
processed = processArg(7);
What does this program do? // Setup var lastName = "Lovelace";
// Only change code below this line. var lastLetterOfLastName = lastName[lastName.length - 1];
It sets the lastLetterOfLastName variable to the last letter in the lastName variable’s string. In this case it would be the letter “e”.
What is a nested array called?
Multi-dimensional Array
How is data accessed inside of an array? What encapsulates this data?
Accessed by indexes. Brackets [ ] are what encapsulate the indexes of an array.
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];
arr[3]; // equals [[10,11,12], 13, 14]
arr[3][0]; // equals [10,11,12]
arr[3][0][1]; // equals 11
What does the function .push() do?
What is arr now?
var arr = [1,2,3];
arr.push(4);
.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.
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();
.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];
What does the function .shift() do?
What does ourArray equal now? var ourArray = ["Stimpson", "J", ["cat"]]; removedFromOurArray = ourArray.shift();
.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”];
What does the function .unshift() do?
What do the variables below equal now?
var ourArray = [“Stimpson”, “J”, “cat”];
ourArray.shift();
ourArray.unshift(“Happy”);
.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”]