简体   繁体   English

如何检查密码是否包含 integer 并返回 object 以及该值的 boolean 表示

[英]how to check if a password contains an integer and return an object with a boolean representation of that value

Trying to create a function that checks a password for integers and then return an object which will store a boolean representing whether the password contains an integer, and then destructure the boolean out of the function, but I keep getting this problem...尝试创建一个 function 来检查整数密码,然后返回一个 object,它将存储一个 boolean,表示密码是否包含 integer,然后从 function 中解构 boolean 我会得到这个问题......但是

 function checkForInteger(password) { const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; let intBoolean = {}; arrayOfIntegers.forEach((int) => { if (password.includes(int)) { intBoolean = { integerIsPresent: password.includes(int) }; } else { intBoolean = { integerIsPresent: password.includes(int) }; } }); return intBoolean; } checkForInteger("3gufr"); //returns {integerIsPresent:false}

You're replacing intBoolean each time through the loop.您每次都在循环中替换intBoolean So if 3 is found, you'll set it to {integerIsPresent: true} on that iteration, but then replace it with {integerIsPresent:false} on the remaining iterations.因此,如果找到3 ,您将在该次迭代中将其设置为{integerIsPresent: true} ,然后在其余迭代中将其替换为{integerIsPresent:false}

Initialize it to false before the loop, and only set it to true when you get a match.在循环之前将其初始化为false ,只有在匹配时才将其设置为true

 function checkForInteger(password) { const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; let intBoolean = { integerIsPresent: false }; arrayOfIntegers.forEach((int) => { if (password.includes(int)) { intBoolean.integerIsPresent = true; } }); return intBoolean; } console.log(checkForInteger("3gufr"));

You can also simplify it by using Array.some()您还可以使用Array.some()来简化它

 function checkForInteger(password) { const arrayOfIntegers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; let intBoolean = { integerIsPresent: arrayOfIntegers.some(int => password.includes(int)) }; return intBoolean; } console.log(checkForInteger("3gufr"));

or you can use a regular expression.或者您可以使用正则表达式。

 function checkForInteger(password) { return { integerIsPresent: .;password.match(/\d/) }; } console.log(checkForInteger("3gufr"));
password.match() returns an array when there's a match. password.match()匹配时返回一个数组。 !! converts that to a boolean. 将其转换为 boolean。

You could probably adjust your logic a little.您可能可以稍微调整一下逻辑。 forEach doesn't allow to you break out of the loop - for/of does. forEach不允许您跳出循环 - for/of可以。

Loop over the array.遍历数组。 If there is a match return the object containing the match, otherwise return an object with a false value.如果存在匹配项,则返回包含该匹配项的 object,否则返回具有假值的 object。

 const arrayOfIntegers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; function checkForInteger(password) { for (const el of arrayOfIntegers) { if (password.includes(el)) { return { integerIsPresent: true }; } } return { integerIsPresent: false }; } console.log(checkForInteger('3gufr')); console.log(checkForInteger('nnh5dd')); console.log(checkForInteger('abcdef'));

this is how I resolved the issue though, thanks a lot @Barmar这就是我解决问题的方法,非常感谢@Barmar

 function checkForInteger(password) { const arrayOfIntegers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; const checkPassword = (el) => password.includes(el); const integerIsPresent = arrayOfIntegers.some(checkPassword); // console.log(integerIsPresent); return integerIsPresent; }

try this.尝试这个。

example:例子:

const checkForInteger = (pass) => pass.split('').reduce((acc, ele) => {
    let int = parseInt(ele);
    if(!isNaN(int)) acc.isPresentInt = true;
    return acc;
  }, {isPresentInt: false})

const res = checkForInteger("3gufr");

result:结果:

{ isPresentInt: true }

Took the password, parsed it out by converting it to an array, then looped through the array and checked each character until it found an integer and modified the accumulated value拿到密码,转成数组解析出来,然后循环遍历数组,逐个检查字符,直到找到一个integer,修改累加值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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