我正在编写一个代码来检查数字是否是alendindrome。但是当我检查输出2个数字时,它的到来不同
function allPalindromicPerms(num) {
// find reverse of a number and then
// comapre the reversed number with the main number
var finalNum = 0;
var originalNum = num;
let remainder = 0;
while (num) {
remainder = num % 10;
console.log('remainder' + remainder);
num = num - remainder;
num = num / 10;
finalNum = finalNum * 10 + remainder;
console.log(finalNum);
}
if (finalNum == originalNum) {
console.log("is palindrome");
} else {
console.log("not a palindrome");
}
}
allPalindromicPerms(23456788888765432) // with the the output is coming fine.
allPalindromicPerms(234567888888765432) // but this is not giving me right output.
你能帮我什么错吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为这两个数字都大于 number.max_safe_integer 。
要使用这个数字如此大的数字,最好坚持使用字符串或使用
bigint
。无论如何,成为回文是词汇属性。请参阅以下示例解决方案:
This is because both those numbers are greater than the
Number.MAX_SAFE_INTEGER
.To work with numbers this big, it's better to stick to strings or use the
BigInt
. Being a palindrome is a lexical property anyway.Please see the below example solution:
那是因为此
2345678888888888765432
大于number.max_safe_integer
它是9007199254740991
> number.max_safe_integer
您可以使用 bigint
That's because this
234567888888765432
is bigger thanNumber.MAX_SAFE_INTEGER
which is9007199254740991
You can't expect any math operators to work with numbers bigger than
Number.MAX_SAFE_INTEGER
You could use BigInt or just a string