简体   繁体   中英

Javascript - every() method to check isInteger on array elements

I want to check return true if an array contains all integers or false if not. I am trying to use the every method MDN docs every .

So if given '1234' it will return true and if given '123a' it would return false.

  function validatePIN (pin) {
    pinArray = pin.split("");
      if (pinArray.length === 4 || pinArray.length === 6) {
        if (pinArray.every(Number.isInteger()) === true;) {
          return true
    }};

How does every pass the element to isInteger so it can test it?

Even if you fix syntax error and pass Number.isInteger as a function this won't work.

 function wrongValidatePIN (pin) { var pinArray = pin.split(""); // <-- array of strings if (pinArray.length === 4 || pinArray.length === 6) { if (pinArray.every(Number.isInteger)) { // <-- isInteger works with numbers return true }} return false } console.log(wrongValidatePIN('1234'))

You need something like this

 function validatePIN (pin) { var pinArray = pin.split(""); // <-- array of strings return (pinArray.length === 4 || pinArray.length === 6) && pinArray.every(char => !Number.isNaN(Number.parseInt(char, 10))) } console.log(validatePIN('1234'), validatePIN('123a'))

Or you could use regexp

 function validatePin(pin) { return /^(\\d{4}|\\d{6})$/.test(pin) } console.log(validatePin('1234'), validatePin('123456'), validatePin('12345'), validatePin('123a'))

As the comments stated, the isInteger function can be passed as an argument by calling pinArray.every(Number.isInteger) instead of calling it a single time, (or by providing it inside a function .every(c=>Number.isInteger(c)) , but passing the function itself is more concise)

However, it's unlikely to provide the result you expect, because isInteger checks if the value is an actual integer, not if the value can be parsed to one.

That could be resolved with something like pinArray.map(parseFloat).every(Number.isInteger);

but then it would be easier to use !pinArray.some(isNaN)

That could make the function:

function validatePIN (pin) {
    return (pin.length === 4 || pin.length === 6) 
        && ![...pin].some(isNaN);
}

But as a final note, regular expressions are made for this type of check and could be preferable here

Your pin.split("") will never work when you pass number eg. 1234 as argument. Convert to string first then split pin.toString().split("") .

Now inside .every() function we cast our string to number by doing +number .

return pinArray.every(number => Number.isInteger(+number));

.every() returns true or false.

Here is working example.

 function validatePIN(pin) { var pinArray = pin.toString().split(""); if (pinArray.length === 4 || pinArray.length === 6) { // returns true or false // +number casts string into number return pinArray.every(number => Number.isInteger(+number)); } // We assume that every PIN is not valid return false; }; console.log('test1', validatePIN(1234)); // true console.log('test2', validatePIN('1234')); // true console.log('test3', validatePIN('123a')); // false console.log('test4', validatePIN('0001')); // true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM