找到由两个3位数字的产品制成的最大的回文 - Javascript [英] Find the largest palindrome made from the product of two 3-digit numbers - Javascript
问题描述
任何人都可以告诉我代码有什么问题。查找由两个3位数字的乘积制成的最大回文
。
Can anyone tell me what's wrong with the code. Find the largest palindrome
made from the product of two 3-digit numbers.
function largestPalindrome(){
for(var i =999; i>100; i--){
for(var j = 999; j>100; j--){
var mul = j*i;
if(isPalin(mul)){
return i * j;
}
}
}
}
function isPalin(i){
return i.toString() == i.toString().split("").reverse().join("");
}
console.log(largestPalindrome());
这个答案接近我的问题
但我仍然觉得我在做循环的方式它应该给我带来最大的产品。
This answer was close to my question but still i feel the way i am doing the loop it should return me the largest product.
推荐答案
你的工作不正常,因为它检查 999 * 999
,然后 999 * 998
,然后 999 * 997
,直到达到 999 * 583
。虽然它没有检查 997 * 995
或更接近顶部
的东西,它产生一个更大的数字
Yours doesn't work properly since it checks 999*999
, then 999*998
, then 999*997
until it reaches about 999*583
. While it doesn't check 997*995
or something closer to the top
which generates a larger number
function largestPalindrome(){
var arr = [];
for(var i =999; i>100; i--){
for(var j = 999; j>100; j--){
var mul = j*i;
if(isPalin(mul)){
arr.push(j * i);
}
}
}
return Math.max.apply(Math, arr);
}
function isPalin(i){
return i.toString() == i.toString().split("").reverse().join("");
}
console.log(largestPalindrome());
这是另一种方法,存储所有回文
由数组中的3个数字生成,然后在数组上使用 Math.max来获得最大的
回文
Here is another approach, store all palindrome
generated by 3 numbers in an array, then use Math.max on the array
to get the largest palindrome
这篇关于找到由两个3位数字的产品制成的最大的回文 - Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!