Chapter 2. Implement program flow Flashcards
array.join method
Accepts a string as a parameter whichj is used as a delimiter to separate the values in the array
new Array('socer', 'Basketball','hockey').join(', ') "socer, Basketball, hockey"
array.reverse()
reverses the array
array.slice()
var sports = new Array('soccer', 'basketball', 'hockey', 'football', 'cricket', 'rugby', 'tennis', 'badminton'); var someSports = sports.slice(1, 2);
The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included).
array.splice()
changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
splice(whereItShouldStart,numberOfItemsToSplice, itemsWhichReplace)
the optional parameter doesent need to be the same length
array.shift() and array.unshift()
Just like Queue and Dequeue
array.some()
checks whether any objects meet the criteria (callback)
var evenNumbers = new Array(0, 2, 4, 6, 8, 9, 10, 12); var allEven = evenNumbers.some(evenNumberCheck, evenNumbers); if (allEven) { ... } else { ... } function evenNumberCheck(value, index, array) { return (value % 2) == 0; }
array.reduce() and array.reduceRight()
The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.
The reduceRight goes from the right side of the array
the break keyword in a loop
The break keyword breaks out of only the currently running loop. If the loop containing the break is nested inside another loop, the outer loop continues to iterate as controlled by its own expression.
How can you hook up an event?
- Declare it directly in the HTML markup
- Assign the function to the event property of the element object through javascript
- Use the newer add and remove methods on the element object to associate event handlers
Exception object properties
- message
- number
- Name
var
Declares a variable, optionally initializing it to a value.
let
declares a block-scoped, local variable, optionally initializing it to a value
const
declares a block-scoped, read-only named constant
arguments in Javascript functions
add(2, 3, 4); // 5 // added the first two; 4 was ignored
arguments object (in a function)
Functions have an additional variable inside their body called agruments which is an array-like object hodling all the values passed into the function
function add() { var sum = 0; for (var i = 0, j = arguments.length; i < j; i++) { sum += arguments[i]; } return sum; }
array.length
array.length isnt necessary the number of items in the array.
var a = ['dog', 'cat', 'hen']; a[100] = 'fox'; a.length; // 101
array.length is one more than the highest index in the array
Objects - two ways of initializing an object
- var obj = new Object();
- var obj = {}
The second one is more convinient
Rest parameter syntax
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
function avg(...args) { var sum = 0; for (let value of args) { sum += value; } return sum / args.length; }
avg(2, 3, 4, 5); // 3.5
Person.prototype
function Person(first, last) { this.first = first; this.last = last; } Person.prototype.fullName = function() { return this.first + ' ' + this.last; }; Person.prototype.fullNameReversed = function() { return this.last + ', ' + this.first; };
Person.prototype is an object shared by all instances of Person. Any time you attempt to access a property that isnt set, Javascript will check Person.prototype and it becomes avaliable to all instances of that constructor via the this object