Search code examples
javascriptarrayssplitfindinclude

Find Number in Array (two-digit Number)


I try to find the Number 7 in an Array and return true (Doesn´t matter if it´s 7, 47 or 507)

The Array: [17, 23, 9, 590]

I tried to use arr.includes(7) but this just returns the Number 7. (So it returns false in this case because there is no 7 in the Array - only 17)


Solution

  • The problem is the comparison number === 7, you should compare each digit instead.

    You can use the function Array.prototype.some, and the function String.prototype.includes to check for a specific digit/char.

    This approach finds the number 7 over each number's digit.

    console.log([17, 23, 9, 590].some(n => String(n).includes(7)))