function contains(number, digit) {
if (number < 0) { // make sure negatives are dealt with properly, alternatively replace this if statement with number = Math.abs(number)
number *= -1;
}
if (number == digit) { // this is to deal with the number=0, digit=0 edge case
return true;
}
while (number != 0) { // stop once all digits are cut off
if (number % 10 == digit) { // check if the last digit matches
return true;
}
number = Math.floor(number / 10); // cut off the last digit
}
return false;
}
let n = 1234;
let flag = false;
while (n > 0){
r = n % 10;
if(r == 1){
flag = true;
break;
}
n = (n - (n % 10)) / 10;
}
console.log("n contains 1 = "+flag);
5条答案
按热度按时间t3psigkw1#
请参考以下代码(如果注解不够好,请随时询问):
d5vmydt92#
这是一个简单的递归形式-
pokxtpni3#
试试这个。
vxbzzdmp4#
试试这个:
bwleehnv5#