JavaScript中数字的平方串联
我们需要编写一个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
热门推荐
10 元旦联谊祝福语简短精辟
11 简短的出行工作祝福语
12 周年蛋糕祝福语简短英语
13 明信片祝福语简短句子
14 国庆简短幽默祝福语
15 好朋友夫妻祝福语简短
16 到同事新家简短祝福语
17 孙子结婚祝福语大全简短
18 祝美术老师祝福语简短