我们需要编写一个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