Basic Flashcards
push()
Adds item to the end of an array.
i.e. arr.push(“name”);
pop()
Removes item from the end of an array.
i.e. arr.pop();
item can also be assigned to a variable.
i.e. myVar = arr.pop();
shift()
Adds item to the front of an array.
i.e. arr.shift(“name”);
unshift()
Removes item from the front of an array.
i.e. arr.unshift();
item can also be assigned to a variable.
i.e. myVar = arr.unshift();
length
Can be used for the length of a string or an array.
i.e. myString.length
===
Strict Equality tests both data and type. It will not convert a string to a number like == will.
!==
Strict Inequality tests both data and type. It will not convert a string to a number like != will.
How are case statements evaluated?
With strict equality, ===
What are the two ways to access the properties of an object?
Dot Notation: myObj.property; or Bracket Notation: myObj["property"]; Bracket notation can also be used with a variable. myProp="property"; myObj[myProp];
How do you delete a property from an object?
delete myObj.property;
How to you add a property to an object?
myObj.myProp = “property”;
How do you check if a property exists on an object?
myObj.hasOwnProperty(“property”);
returns true if property exists.
How do you generate a random number?
Math.random();
Returns a whole number between 0 and almost 1, but it doesn’t actually get to 1.
How do you round down to the next whole number?
Math.floor();
How do you generate a whole number between 0 and 9?
Math.floor(Math.random() * 10);
How do you generate a random whole number within a range?
Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
How do you initialize and object with a constructor?
var MotorBike = function() { this.wheels=2; this.engines=1; this.seats=1; };
describe the map function.
The map function iterates through an array and modifies each item by the function.
describe the filter function
The filter function iterates through an array and creates a new array with items that match the filter callback.
//this filter creates a new array that includes all values not equal to 5. array = array.filter(function(val) { return val !== 5; });
describe the sort function
The sort function sorts the array in place and uses a text sort if no parameters are passed. Passing in two paramaters lets you sort numbers in either direction.
//sort from largest to smallest array.sort(function(a,b){ return b-a; });
//sort from smallest to largest array.sort(function(a,b){ return a-b; });
describe reverse function
The reverse function reverses the order of items in an array in place.
myArray.reverse();
but can also return a reversed array
newArray = array.reverse();
describe concat function
The concat function creates a new array by combining two arrays together.
newArray = oldArray.concat(otherArray);
Describe the slice function.
The slice function can work on strings or arrays. It can slice a substring from a string into a new string or it can slice a section of an array into a new array. The parameters are Start and End.
var a = ['zero', 'one', 'two', 'three']; var sliced = a.slice(1, 3);
Describe the splice function.
Splice can either insert items into an array at a specific point or it can delete items from an array starting at an index and removing the specified number.
var myFish = [‘angel’, ‘clown’, ‘mandarin’, ‘sturgeon’];
myFish.splice(2, 0, 'drum'); // insert 'drum' at 2-index position // myFish is ["angel", "clown", "drum", "mandarin", "sturgeon"]
myFish.splice(2, 1); // remove 1 item at 2-index position (that is, "drum") // myFish is ["angel", "clown", "mandarin", "sturgeon"]