Step 10
The outcome array (from the previous section) is an array of strings (characters). We need to convert those characters to numbers. This can be done using array's map
method. The map()
operation creates a new array populated with the results of calling a provided function on every element in the calling array.
const cnumber = "4003600000000014"; let arr = cnumber.split(''); arr = arr.reverse(); arr = arr.map(convertToInt) function convertToInt(element) { return parseInt(element); } console.log(arr);
Notice convertToInt
is a function that is passed as argument to another function (i.e., map()
).
A higher-order function is a function that takes a function as an argument (or returns a function).
The array's map
is a higher-order function.
JavaScript supports both higher-order functions and first-order functions (those you worked with, in Java and C++).