-1

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 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}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Keep getting what problem? – Barmar May 04 '22 at 22:00
  • 2
    You're overwriting `intBoolean` each time through the loop. The final value is just whether it contains the last element in the array. So you're just checking whether the password contains `9`. – Barmar May 04 '22 at 22:02
  • Like Barmar says, you cannot change the result each time you check a single digit. Since you are looking for any match, you need to start by assuming the result is `false`, then iterate over the array of integers and if you find a match, change the result to `true`. –  May 04 '22 at 22:05
  • Also note that your if block nonsensical since it does the exact same thing whether the test passes or not, the first indication that your logic is off. –  May 04 '22 at 22:06
  • @Ba i think you're right – Uwa Gideon May 04 '22 at 22:10
  • Duplicate: [Check if an array contains any element of another array in JavaScript](https://stackoverflow.com/questions/16312528/check-if-an-array-contains-any-element-of-another-array-in-javascript) –  May 04 '22 at 22:15
  • Here's the dupe for a string including any array elements, although the logic and code is the same https://stackoverflow.com/questions/5582574/how-to-check-if-a-string-contains-text-from-an-array-of-substrings-in-javascript –  May 04 '22 at 22:18

4 Answers4

1

You're replacing intBoolean each time through the loop. 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.

Initialize it to false before the loop, and only set it to true when you get a match.

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()

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. !! converts that to a boolean.
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You could probably adjust your logic a little. forEach doesn't allow to you break out of the loop - for/of does.

Loop over the array. If there is a match return the object containing the match, otherwise return an object with a false value.

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'));
Andy
  • 61,948
  • 13
  • 68
  • 95
0

this is how I resolved the issue though, thanks a lot @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;
}
Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
0

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

Nelsongt77
  • 48
  • 1
  • 3