Search code examples
javascriptnumbersboolean

Boolean if variables have all similarities


I have 2 numbers and I would like to compare them. If all number B appears in A = true else = false

I've tried this but it's still returning True when B has a number that doesn't exist in A

  if (A >= B)
    return true
  else
    return false

ex:

    A = 98765
    B = 8759
    A >= B // True
    
    A = 154
    B = 154
    A >= B // True

    A = 875
    B = 8756
    A >= B // False

    A = 1111
    B = 112
    A >= B //output: True // expected output: False because don't have 2 in A

Solution

  • You may do the following steps:

    1. convert each number into a string with toString()
    2. convert each string into its individual characters with .split('')
    3. check for every character in B if it is included in the characters of A
    const aChars = A.toString().split('')
    const bChars = B.toString().split('')
    
    return bChars.every(c => aChars.includes(c))