如何在Palindromes类的getter方法中访问静态方法的返回值?
class Palindromes { static generate(obj) { const arr = []; const arrPalindrome = []; let j = obj.minFactor; while (j <= obj.maxFactor) { for (let i = obj.minFactor; i <= obj.maxFactor; i++) { if (!arr.includes(j * i)) { arr.push(j * i); } } j++; } arr.forEach((el) => { if (el === +String(el).split("").reverse().join()) { arrPalindrome.push(el); } }); return arrPalindrome; } get smallest() { return Palindromes.generate(obj); //I am using the return value of the STATIC method above to do some calc here. } get largest() {} } const result = Palindromes.generate({ minFactor: 1, maxFactor: 9 }); console.log(result.smallest);
问题背景
检测给定范围内的回文数乘积。
回文数(palindromic number)是指数字反转后与原数相同的数。例如,121是回文数,而112不是。
给定一个数字范围,找出该范围内两个数乘积所得的最大和最小回文数。
你的解决方案需要返回最大和最小回文数,以及它们在该范围内的所有因数对。如果最大或最小回文数有多组符合条件的因数对,则返回所有因数对。
给定范围[1, 9](包含两端):
- 该范围内所有可能的乘积列表为:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]
- 其中的回文数乘积为所有个位数:[1, 2, 3, 4, 5, 6, 7, 8, 9]
- 最小回文数乘积是1,其因数对为(1, 1)
- 最大回文数乘积是9,其因数对为(1, 9)和(3, 3)
我需要在上述类的smallest和largest getter方法中访问静态方法generate的返回值,以完成相关计算。
修复后的实现
你的代码存在几个核心问题:
smallestgetter中的obj未定义,实例无法直接获取静态方法的传入参数- 调用
Palindromes.generate返回的是数组,数组没有smallest属性,所以console.log(result.smallest)会输出undefined - 原
generate方法只收集回文数,未保存对应的因数对,无法满足返回因数对的需求
重构后的完整实现如下:
class Palindromes { constructor({ minFactor, maxFactor }) { this.minFactor = minFactor; this.maxFactor = maxFactor; // 预生成回文数与对应因数对的映射 this.palindromeData = this.buildPalindromeData(); } // 生成回文数到因数对的映射表 buildPalindromeData() { const dataMap = new Map(); // 从i开始遍历j,避免重复因数对(如(1,9)和(9,1)视为同一组) for (let i = this.minFactor; i <= this.maxFactor; i++) { for (let j = i; j <= this.maxFactor; j++) { const product = i * j; if (this.isPalindrome(product)) { if (!dataMap.has(product)) { dataMap.set(product, []); } dataMap.get(product).push([i, j]); } } } return dataMap; } // 辅助方法:判断是否为回文数 isPalindrome(num) { const str = String(num); return str === str.split('').reverse().join(''); } get smallest() { const minVal = Math.min(...this.palindromeData.keys()); return { value: minVal, factors: this.palindromeData.get(minVal) }; } get largest() { const maxVal = Math.max(...this.palindromeData.keys()); return { value: maxVal, factors: this.palindromeData.get(maxVal) }; } // 保留原静态方法,返回所有回文数数组 static generate({ minFactor, maxFactor }) { const instance = new Palindromes({ minFactor, maxFactor }); return Array.from(instance.palindromeData.keys()); } } // 使用示例 const palindromeInstance = new Palindromes({ minFactor: 1, maxFactor: 9 }); console.log(palindromeInstance.smallest); // { value: 1, factors: [[1, 1]] } console.log(palindromeInstance.largest); // { value: 9, factors: [[1, 9], [3, 3]] } // 调用静态方法获取所有回文数 const allPalindromes = Palindromes.generate({ minFactor: 1, maxFactor: 9 }); console.log(allPalindromes); // [1,2,3,4,5,6,7,8,9]
关键改进说明:
- 新增
constructor接收范围参数并保存为实例属性,让smallest和largest可以直接访问 - 实现
buildPalindromeData方法,生成回文数与因数对的映射,同时避免重复因数对 smallest和largestgetter从映射中提取对应数据,返回包含value(回文数)和factors(因数对数组)的结构,完全符合需求- 保留原静态
generate方法,兼容原有调用逻辑 - 提取
isPalindrome辅助方法,让代码逻辑更清晰
内容的提问来源于stack exchange,提问作者Afzal18
相关产品推荐
相关产品推荐

