将数字的每个数字平方-JavaScript
我们需要编写一个JavaScript函数,该函数接受一个数字并返回一个新数字,在该数字中,原始数字的所有数字均被平方并连接在一起
例如:如果数字是-
9119
那么输出应该是-
811181
因为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 教师祝福语简短8字
12 六一祝福语简短句子
13 给新娘简短的祝福语
14 小乖生日祝福语 简短
15 2026对老师祝福语简短
16 最简短美好的祝福语
17 中考送给老师祝福语简短
18 生日祝福语20字简短