Unexpected Behavior Of Bound Function
Tried to create a function mapping numeric characters (i.e. '0' to '9') to true and other characters to false: const isNumeric = String.prototype.includes.bind('0123456789'); isNu
Solution 1:
includes
has a second parameter called position
which is the position within the string at which to begin searching. every
, like every other array prototype methods, provides the index as the second argument to the callback provided. So, the code ends up being something like this:
const exists = ['1', '0'].every((n, i) =>isNumeric(n, i))
// Which translates to// Look for "1" starting from index 0. It is found// Look for "0" starting from index 1. Fails because "0" is at index 0const exists = ['1', '0'].every((n, i) =>'0123456789'.includes(n, i))
Here's a snippet:
const isNumeric = String.prototype.includes.bind('0123456789'),
numbers = Array.from('149563278'); // array of numbers in random orderconsole.log( numbers.every(isNumeric) ) // falseconsole.log( numbers.every(n =>isNumeric(n)) ) // true
Post a Comment for "Unexpected Behavior Of Bound Function"