more on funcs, arrays,bind,CB, forEach,map(),filter,reduce,DOM Flashcards
how can i bind a param to a func? and wha t is the point of binding a func?
create a funcparam now create a binded func: let bound = OGfuncName.bind(null, prevalent value)
note: i set null because in this case the “this” keyword isnt used
2. binding a func is usefull if a have a certain param that is used frequently!
basically im creating a new func just for a special param
provide an example on a CB func that acts like a bind func.
- create an OGfunc with a param
- return the CB with a parm
- RETURN the expression
- berryup the OGfunc with a param
- func the berry and paramize it; then call it
const taxRater = function (rate) { return function (value) { return value * rate + value; }; };
const getTax = taxRater(0.1); console.log(getTax(200));
how can bind be useful with objts? describe an example.
Bind can be useful if a have abunch of objts that require the same method:
const myMethod = ogMethod.bind(otherObj) myMethod( otherParam, otherParam1)
NOTE: Console.log(otherOBJ) to see the results
Why is bind useful with eventHandlers? describe what an event handler function loos like?
the handler func “this” keyword always points to the html element, not the obj we want.
Fix:
document.querySelector(‘btn-name’).addEventListener(‘click’, obj.method.bind( obj )
arr = [1,2,3,4,5]
what does: arr.splice(1, 2) prnt?
why is slice cool?
[1, 4, 5]
It is good for deleting unwanted items. especially the last item
how can i get the last item of an array?
can i use at with strs?
arr. at( -1 )
yessir: CL(‘jonas’.at( 0 )
what old fashion method does the forEach() replace?
give an example please.
it replaces the for loop if/else block.
let x = array.forEach(function(item, i, array) { return (expression) } )
How can i upload a list child element to a webpage?
(container name) which is…..
doc.QS-tag berry.insertAdjacentHTML(‘afterbegin’, htmlStringBerry)
How can i delete container elements from a webpage?
container name.innerHTML = ‘ ‘;
What does map( ) do? Provide an example.
map method creates a new array with items that have undergone an operation. Its a CB func.
const newArr = oldArr.map(function(item, i, array) { return item * usdToEuroBerry} )
this returns a new array with all items updated according to the operation specified.
what does filter do? Example plz.
filter method revises an array based on a condition
const positiveNums = +&-Array.filter(function(item, i ) { return item > 0 } )
describe the “creating a username forEach method” that jonas taught you.
we are cycling an array of objs and adding a prop to each obj in the array
const createUser = function(accs) {
accs. forEach(function(acc ) { accs. username = accs.owner.toLowerCase( ).split(' ').map( name => name[ 0 ].join()
createUser( arrayOfaccountObjs)
what does the reduce method do? example plz
reduce method boils all items in an array down to one value via the snowball accumulator:
const x = array.reduce(function(acc, item, i, array) { return acc + cur}, 0 );
this gives the sum of all numbers. NOTE: remember the accumulator start point parameter