For example if the number 752 contains the number 5? Whats the best way to check? Convert to string or divide into individual digits?
bool containsDigit(int number, int digit); If number contains digit, then the function should return true . Otherwise, the function should return false .
We call a natural number a duodigit if its decimal representation uses no more than two different digits. For example, , and are duodigits, while is not. It can be shown that every natural number has duodigit multiples. Let be the smallest (positive) multiple of the number that happens to be a duodigit.
Convert to string and use indexOf
(752+'').indexOf('5') > -1
console.log((752+'').indexOf('5') > -1);
console.log((752+'').indexOf('9') > -1);
Convert to string
and use one of these options:
indexOf()
:
(number + '').indexOf(needle) > -1;
includes()
:
(number + '').includes(needle);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With