JavaScript 中数字的平方连接
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/8 23:08:17
我们需要编写一个 JavaScript 函数,该函数接受一个数字并返回一个新数字,其中原始数字的所有数字都经过平方并连接。
例如:如果数字是 −
99
那么输出应该是 −
8181
因为 9^2 是 81 而 1^2 是 1。
因此,让我们编写该函数的代码 −
示例
其代码为 −
const num = 9119;
const squared = num => {
const numStr = String(num);
let res = '';
for(let i = 0; i < numStr.length; i++){
const square = Math.pow(+numStr[i], 2);
res += square;
};
return res;
};
console.log(squared(num));
输出
控制台中的输出将是 −
811181
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

