Javascript describes the map method syntax as:
arr.map(callback[, thisArg])
Parameters
- callback - Function that produces an element of the new Array, taking three arguments:
- currentValue -The current element being processed in the array.
- index - The index of the current element being processed in the array.
- array The array map was called upon.
- thisArg - Value to use as this when executing callback.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
I can write a JS function that capitalizes the first letter of each word in an array of strings as follows:
capyyIt(['john', 'nelson mandela', 'james booth']) // returns ['John', 'Nelson mandela', 'James booth']
function capyyIt(names) {
return names.map(function capIt(curr) { return curr[0].toUpperCase() + curr.slice(1).toLowerCase(); });
}
- How can I write capIt with 4 parameters (even though I may not be using them all)? I'm asking because I tried it with 4 parameters but it didn't seem to work.
How can I write capIt as a non-immediately invoked function expression - something like:
function capyyIt(names) { return names.map(capIt); } function capIt(curr,index,arr) { return curr[0].toUpperCase() + curr.slice(1).toLowerCase(); }